How to Correctly Display ManyToMany ID Matching List in Django?

WayBehind

How to correctly display ManyToMany ID matching list?

My report list (weekly, monthly etc):

Car Number 1:
-customer (date, name etc) 
-customer (date, name etc)

Car Number 2:    
-customer (date, name etc) 
-customer (date, name etc) 

My Models:

class Car(models.Model):
    name ...

class Reservation(model.Models):
    cars = models.ManyToManyField('Car', related_name="reservation")
    customerinfo = ...

As each customer can reserve multiple cars, I have to use M2M field.

Now displaying the list :

{% for car in cars %}
<h1>{{ car.name }}</h1>
    {% for reservation in reservations %}
        {% for reservationcar in reservation.cars.all %}
            {% if reservationcar.id = car.id %}
                <h2>{{ reservation.cusotmerinfo }}</h2>
            {% endif %}
        {% endfor %}
    {% endfor %}
{% endfor %}

Now, the above works, but doesnt look right and looks quite dirty.

Is there a better, cleaner, shorter way to accomplish that?

EsseTi

The M-M is "wrong" you can user this It will looks like something like this (not tested)

class User(models.Model):
    .. any data you need ..

class Car(models.Model):
    .. any data you need ..
    reservations = models.ManyToManyField(User, through='Reservations')


class Reservations(models.Model):
    car = models.ForeignKey(Car)
    user = models.ForeignKey(User)
    .. any data you need ..

You can probably place the reservations in the user and change the ref.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Django: How to query objects where manytomany fields ALL exist in a list?

From Dev

Display items with matching id

From Dev

Using ManyToMany through correctly for complex model in django

From Dev

How to get my iframe code to display correctly in django cms?

From Dev

How to correctly display the date of a list from Cloud Firestore?

From Dev

How to find duplicate id in table and display list of that

From Dev

How to display a list in django tables 2

From Dev

How to fix manytomany field in django

From Dev

how reverse query ManytoMany Django

From Dev

How to Query Django ManyToMany Relationship

From Dev

How to display table correctly?

From Dev

Add an object by id in a ManyToMany relation in Django

From Dev

django admin list_display foreign key id without join

From Dev

django admin list_display foreign key id without join

From Dev

Save list to manytomany field not using the ID field

From Dev

Django ManyToMany indirect lookup from list

From Dev

Django ManyToMany indirect lookup from list

From Dev

Django: How correctly show list of objects in related models?

From Dev

How to retrieve data from manytomany table in python django based on its ID

From Dev

Django 1.6 Cant display a manytomany field from models in template

From Dev

How to display timestamp on QT correctly?

From Dev

Cannot get my bullet list to display correctly

From Dev

How to add an object to a manytomany field in django

From Dev

How to join ManyToMany querysets in Django/Python

From Dev

How to filter some items in a manytomany field in Django

From Dev

How to set value of a ManyToMany field in Django?

From Dev

How to join ManyToMany querysets in Django/Python

From Dev

Django: how to filter a model with ManyToMany relationships

From Dev

Django/python How to get a list of id's from a list of Objects

Related Related

  1. 1

    Django: How to query objects where manytomany fields ALL exist in a list?

  2. 2

    Display items with matching id

  3. 3

    Using ManyToMany through correctly for complex model in django

  4. 4

    How to get my iframe code to display correctly in django cms?

  5. 5

    How to correctly display the date of a list from Cloud Firestore?

  6. 6

    How to find duplicate id in table and display list of that

  7. 7

    How to display a list in django tables 2

  8. 8

    How to fix manytomany field in django

  9. 9

    how reverse query ManytoMany Django

  10. 10

    How to Query Django ManyToMany Relationship

  11. 11

    How to display table correctly?

  12. 12

    Add an object by id in a ManyToMany relation in Django

  13. 13

    django admin list_display foreign key id without join

  14. 14

    django admin list_display foreign key id without join

  15. 15

    Save list to manytomany field not using the ID field

  16. 16

    Django ManyToMany indirect lookup from list

  17. 17

    Django ManyToMany indirect lookup from list

  18. 18

    Django: How correctly show list of objects in related models?

  19. 19

    How to retrieve data from manytomany table in python django based on its ID

  20. 20

    Django 1.6 Cant display a manytomany field from models in template

  21. 21

    How to display timestamp on QT correctly?

  22. 22

    Cannot get my bullet list to display correctly

  23. 23

    How to add an object to a manytomany field in django

  24. 24

    How to join ManyToMany querysets in Django/Python

  25. 25

    How to filter some items in a manytomany field in Django

  26. 26

    How to set value of a ManyToMany field in Django?

  27. 27

    How to join ManyToMany querysets in Django/Python

  28. 28

    Django: how to filter a model with ManyToMany relationships

  29. 29

    Django/python How to get a list of id's from a list of Objects

HotTag

Archive