#serialize confusion

11 messages ยท Page 1 of 1 (latest)

dark oak
#

Hello, i'm new to django
i m a confused about serialization

**in my template (cart.html): i want to access data of orderItem ( quantity, order..) and also product data in same time (price, name...) **

note that in my database: the two tables are related, and we can see a foreign key in models :

here is my code:

lets imagine a serialization file : ```python

serializers.py

class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = ('name', 'price', 'description')

python class OrderItemSerializer(serializers.ModelSerializer):
class Meta:
model = OrderItem
fields = ('item', 'order', 'quantity')


```python
# models.py

class Product(models.Model):
    name = models.CharField(max_length=100, null=True)
    price = models.FloatField()
    description = models.TextField()

    # image = models.ImageField(upload_to='')

    def __str__(self):
        return self.name

class OrderItem(models.Model):
    item = models.ForeignKey(Product, on_delete=models.SET_NULL, blank=True, null=True)
    order = models.ForeignKey(Order, on_delete=models.SET_NULL, blank=True, null=True)
    quantity = models.IntegerField(default=0, null=True, blank=True)
    date_added = models.DateTimeField(auto_now_add=True)
# views.py 

def cart(request):
    if request.user.is_authenticated:
        customer = request.user.customer
        order, created = Order.objects.get_or_create(customer=customer, ordered=False)
        items = order.orderitem_set.all()
    else:
        items = []

    context = {"items": items}
    return render(request, 'market/cart.html', context)

{% for item in items %}
  <tr>
      <th>{{ item.id }}</th>
      <td>{{ item.name }}</td>
      <td>{{ item.price }}</td>
      <td>{{ item.quantity }}</td>
  </tr>
{% endfor %}
leaden sentinel
#

If your using templates and views, you don't necessarily need Serializers at all.

dark oak
# leaden sentinel If your using templates and views, you don't necessarily need Serializers at all...

after finishing the website, i ll need to create a mobile app for it

and Pat said: Serialization is useful to have the data in a different format, e.g. you can just use JSONResponse or django-rest-framework to return JSON data. Then instead of using django templates, **you can use a JavaScript framework like vue.js to build a single-page application, a native Android app, or a script as a client**

leaden sentinel
#

Nothing in your post mentioned a mobile app...

dark oak
#

yeah , sorry

hasty skiff
#

You can have both templates and an API ๐Ÿ™‚

#

But then there's no need to use the serializer with views that render templates.

dark oak
leaden sentinel
#

If doesn't necessarily need to be another app either. have a file with your serializers and then some new views which then use them to expose the data over new urls which your app would use.

dark oak
#

humm , i see

#

thank you, i ll try this appeach, and will back if i find some issue ๐Ÿ™‚