Question: My django app displays the objects from database in table view. The problem is that these objects (models) are pretty complex: the have 50+ fields. Nearly for each field I have to do some formatting:

* conver phone numbers from int 71234567689 to "+7 (123) 456789"
* display long prices with spaces: "7 000 000" instead of "7000000"
* construct full address from several fields like "street", "house" and so on (logic if pretty complex with several if-else-s)
* and so on

Django templating language has several useful tags for simple cases but I guess is not suitable in general case (like mine) for serious formatting.

Create the @property-s in model class is also not an option because the question is about rendering and is not related to model.

So I guess I should do my conversions in view: create dict for each obj, fill with converted data and pass to template. 

But! The model has a lot of fields and I don't want to copy them all :) Moreover, it would be great to preserve model structure to use it in django template (say, regroup) and query set laziness.

**So the greatest way would be to instruct django "how to render". Is it possible?**

Answer: 
It's caused by an infinite loop when parsing datas to JSON.

You can solve this by using `@JsonManagedReference` and `@JsonBackReference` annotations.

Definitions from API :

* JsonManagedReference (<https://fasterxml.github.io/jackson-annotations/javadoc/2.5/com/fasterxml/jackson/annotation/JsonManagedReference.html>) : 

> 
> Annotation used to indicate that annotated property is part of two-way
>  linkage between fields; and that its role is "parent" (or "forward")
>  link. Value type (class) of property must have a single compatible
>  property annotated with JsonBackReference. Linkage is handled such
>  that the property annotated with this annotation is handled normally
>  (serialized normally, no special handling for deserialization); it is
>  the matching back reference that requires special handling
> 
> 
>
* JsonBackReference: (<https://fasterxml.github.io/jackson-annotations/javadoc/2.5/com/fasterxml/jackson/annotation/JsonBackReference.html>):

> 
> Annotation used to indicate that associated property is part of
>  two-way linkage between fields; and that its role is "child" (or
>  "back") link. Value type of the property must be a bean: it can not be
>  a Collection, Map, Array or enumeration. Linkage is handled such that
>  the property annotated with this annotation is not serialized; and
>  during deserialization, its value is set to instance that has the
>  "managed" (forward) link.
> 
> 
>

Example:

**Owner.java:**

```
@JsonManagedReference
@OneToMany(mappedBy = "owner", fetch = FetchType.EAGER)
Set<Car> cars;

```

**Car.java:**

```
@JsonBackReference
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "owner_id")
private Owner owner;

```

Another solution is to use `@JsonIgnore` which will just set null to the field.
Using FetchType.LAZY , if still getting the error "Could not write content: failed to lazily initialize a collection of role" , that may be probably caused by somewhere in the logic (perhaps in a controller) , Catalog is being tried to be deserialized that contains list of catalog items which is a proxy but the transaction has already ended to get that. 
So create a new model ('CatalogResource' similar to catalog but without the list of items).
Then create a catalogResource object out of the Catalog (which is returned from the query) 

```html
public class CatalogResource {
    private int catalog_id;
    private String name;
    private List<Order> orders;
} 
```