In my app, there is a model "Image" and couple of models that an image can relate to (Event, Place, Member). I am considering two ways of modelling the image relations. The first one is to put relation field in Image for each related model (the field will be either ForeignKey or ManyToManyField). The second way is to put field "images" to each model that can have associated images (in case of some models it will be just one image). Which way is more recommended?
# the first way
class Member(models.Model):
name = models.CharField('name', max_length=128)
class Event(models.Model):
name = models.CharField('name', max_length=128)
class Place(models.Model):
name = models.CharField('name', max_length=128)
class Image(models.Model):
title = models.CharField('title', max_length=128)
members = models.ManyToManyField(Member)
place = models.ForeignKey(Place)
event = models.ForeignKey(Event)
# the second way
class Image(models.Model):
title = models.CharField('title', max_length=128)
class Member(models.Model):
name = models.CharField('name', max_length=128)
images = models.ManyToManyField(Image)
class Event(models.Model):
name = models.CharField('name', max_length=128)
images = models.ManyToManyField(Image)
class Place(models.Model):
name = models.CharField('name', max_length=128)
images = models.ManyToManyField(Image)
I could also use generic relations (third way) or I could make models like EventImage, PlaceImage, MemberImage (fourth way) but I have already decided these would not work for me that well.
If your relationship is Many-to-one, then the proper way to model it is with ForeignKey.
For Many-to-many relationships, it's less clear on which model to define one. Django documentation says the following:
It doesn’t matter which model has the ManyToManyField, but you should
only put it in one of the models – not both.
Generally, ManyToManyField instances should go in the object that’s
going to be edited on a form. In the above example, toppings is in
Pizza (rather than Topping having a pizzas ManyToManyField ) because
it’s more natural to think about a pizza having toppings than a
topping being on multiple pizzas. The way it’s set up above, the Pizza
form would let users select the toppings.
Related
I don't understand why should I use ManyToManyField if I will (or might) store extra information along the many-to-many relationships.
The doc here shows this example:
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=50)
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(
Person,
through='Membership',
through_fields=('group', 'person'),
)
class Membership(models.Model):
group = models.ForeignKey(Group, on_delete=models.CASCADE)
person = models.ForeignKey(Person, on_delete=models.CASCADE)
inviter = models.ForeignKey(
Person,
on_delete=models.CASCADE,
related_name="membership_invites",
)
invite_reason = models.CharField(max_length=64)
Why even having the members field there, if the developer already defines the relationship model and also defines the ForeignKeys? :D Why not leaving out the members ManyToManyField ?? What is the benefit? Thank you.
I don't understand why should I use ManyToManyField if I will (or might) store extra information along the many-to-many relationships.
You do not need to do this. But it makes querying more effective. Indeed, you can now query with:
Group.objects.filter(members=some_person)
this is shorter and more descriptive than working with:
Group.objects.filter(membership__person=some_person)
It is thus a "coding shortcut" to go from the Group model to the Person model and vice versa.
With following Django models:
class Author(models.Model):
name = models.CharField(max_length=30)
bestbookaccordingtome=models.????(Author,null=True, blank=True, default = None)
class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.OnetoOneField(Author)
I want the classic relationship each book has one author (only).
But I also want to be able to assign a chosen book to author (my favourite book of this author for example).
I tried a foreign key but django didn't like it.
Any python clean way to do it?
What you need is ManyToManyField.
class Author(models.Model):
name = models.CharField(max_length=30)
bestbookaccordingtome = models.ManyToManyField('self', symmetrical=False, related_name='best_book_according_to_me')
Also, if you need to specify extra fields in your n-m model, you can use through to indicate the name of the model.
Hope it helps!
I'm having trouble understanding the use of ManyToMany models fields with a through model. I can easily achieve the same without the ManyToMany field. Considering the following from Django's docs:
class Person(models.Model):
name = models.CharField(max_length=128)
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(Person, through='Membership')
class Membership(models.Model):
person = models.ForeignKey(Person, on_delete=models.CASCADE)
group = models.ForeignKey(Group, on_delete=models.CASCADE)
date_joined = models.DateField()
invite_reason = models.CharField(max_length=64)
What I don't understand, is how is using the ManyToMany field better than simply dropping it and using the related manager. For instance, the two models will change to the following:
class Group(models.Model):
name = models.CharField(max_length=128)
class Membership(models.Model):
person = models.ForeignKey(Person, on_delete=models.CASCADE)
group = models.ForeignKey(Group, on_delete=models.CASCADE, related_name='members')
date_joined = models.DateField()
invite_reason = models.CharField(max_length=64)
What am I missing here?
You're right, if you define the membership table explicitly then you don't need to use a ManyToManyField.
The only real advantage to having it is if you'd find the related manager convenient. That is, this:
group.members.all() # Persons in the group
looks nicer than this:
Person.objects.filter(membership_set__group=group) # Persons in the group
In practice, I think the main reason for having both is that often people start with a plain ManyToManyField; realize they need some additional data and add the table explicitly; and then continue to use the existing manager because it's convenient.
So I just wanted to add to anyone who is looking at this and may want another example to save them research. For one, I think it's important to note that in OP's questions, he should of removed the Group model not the People model and removed the matching field from the Membership model. That way, the model goes back to it's original meaning.
When looking at a many-to-many relationship, the through field can almost be contrived as the "why" to the many-to-many relationship. If we give the nomenclature a different name, it might change what the reader sees:
class Person(models.Model):
name = models.CharField(max_length=128)
class Club(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(Person, through='RegistrationReceipt')
class RegistrationReceipt(models.Model):
person = models.ForeignKey(Person, on_delete=models.CASCADE)
club = models.ForeignKey(Club, on_delete=models.CASCADE)
date_joined = models.DateField()
invite_reason = models.CharField(max_length=64)
paid_dues = models.BooleanField(default = True)
fee_payment_date = models.DateTimeField()
Now, you can imagine yourself adding all sorts of logic whenever a member joins this club. When they joined? Why did they join? Did they pay? When is their payment date? etc. You can obviously tackle this relationship in different ways, but you can see more clearly the use of "through" in a Many-to-Many relationship.
Also, for those that know SQL. The through attribute/field is the way you customize the intermediary table, the one that Django creates itself, that one is what the through field is changing.
I have some problem with the answer from Kevin Christopher Henry.
I don't think that the equivalent of the group.members.all() without a through="members" is Person.objects.....
Instead I think it is group.person_set.all() if the M2M field is on Person side. Or group.persons.all() if the M2M field is inside Group.
But I think without through=.. you have no control over the created table. It contains and will contain just 2 fields: both ID's of the related rows.
But with through=.. you can create the model yourself and add (now or later) the additional fields, which often can have a good reason. Example of such field: valid_from = DateField(), or so.
Below is the code block of a Many-To-Many relationship between two models. Having searched and researched there appears to be no elegant way to create custom id column names for the foreign keys in the intermediary (or link) table that Django creates. The custom name using db_table='pizza_link_topping' creates the desired table name but the columns names in the table are not customizable it appears. Django automatically applied an _id to the end of the model name effectively making a column named topping_id and one called pizza_id.
What if I want custom names? Similar to how I can change the table name.
class Topping(models.Model):
topping_id = models.AutoField(primary_key=True)
name = models.CharField(max_length=200)
def __unicode__(self):
return self.name
class Meta:
db_table = 'pizza_topping'
class Pizza(models.Model):
pizza_id = models.AutoField(primary_key=True) #custom id column name
topping = models.ManyToManyField(Topping, db_table='pizza_link_topping')
def __unicode__(self):
return self.title
class Meta:
db_table = 'pizza' #custom table name
https://docs.djangoproject.com/en/1.7/ref/models/fields/#django.db.models.Field.db_column
^Talks about using db_column but that only appears to work for only models.ForeignKey and not for models.ManyToManyField which is what I need.
I read about using through but that seams hackish and not really good...
https://docs.djangoproject.com/en/1.7/ref/models/fields/#django.db.models.ManyToManyField.through
Perhaps I don't understand fully how through works and it is the right way.
through is not so hackish. It does exactly what you want:
class Pizza(models.Model):
...
topping = models.ManyToManyField(Topping, through='PizzaLinkTopping')
class PizzaLinkTopping(models.Model):
pizza = models.ForeignKey(Pizza, db_column='pizza_noid')
topping = models.ForeignKey(Topping, db_column='topping_noid')
class Meta:
db_table = 'pizza_link_topping'
I have four models in my models.py which are:
models.py
class Course(models.Model):
course_code = models.CharField(max_length=100,unique=True)
title = models.CharField(max_length=200)
short = models.CharField(max_length=50)
elective_group = models.CharField(max_length=100)
class Unit(models.Model):
title = models.CharField(max_length=100)
short = models.CharField(max_length=50)
course = models.ForeignKey(Course)
class Pattern(models.Model):
pattern_name = models.CharField(max_length=200)
class ExamSchedule(models.Model):
exam_date = models.DateTimeField()
course = models.ForeignKey(Course)
pattern = models.ForeignKey(Pattern)
**units = models.ManyToManyField(Units)**
I have all these models register with admin site, so that I can use admin functionality for these models.
My problem is when a user creates or edits a ExamSchedule object, I want the units(field) multivalue widget should contains only those values that are associated with a course as every course can have multiple units. So if user creates an Examschedule object and after selecting a course from dropdown the unit widget should only contains those units that related to the course selected.
Thanks
I have used this django plugin to do this exact thing in the admin sections. I believe it also works in the front end as well:
https://github.com/digi604/django-smart-selects