So I am new to Django, and want to describe the scenario: there are a bunch of Persons, and there are a bunch of Items, and a person passes Items to another Person.
I have the following model:
class Item(models.Model):
title = models.CharField(max_length=1024, blank=False)
def __unicode__(self):
return self.title
class Person(models.Model):
name = models.CharField(max_length=127, blank=False)
out_item = models.ManyToManyField(
Item,
through='Event',
through_fields=('from_user', 'item'),
related_name='giver'
)
in_item = models.ManyToManyField(
Item,
through='Event',
through_fields=('to_user', 'item'),
related_name='receiver'
)
def __unicode__(self):
return self.name
class Event(models.Model):
item = models.ForeignKey(Item)
from_user = models.ForeignKey(Person, related_name='event_as_giver')
to_user = models.ForeignKey(Person, related_name='event_as_receiver')
But makemigrations tells me app.Person: (models.E003) The model has two many-to-many relations through the intermediate model 'app.Event'.
I wonder what I did wrong? Or what is a clean way to achieve the scenario? Perhaps I can separate Event into GiveEvent and ReceiveEvent? But that just makes less sense intuitively, since there is actually only a single event when item is passed.
What you're describing sounds reasonable enough. There may be a technical reason why that's disallowed; one semantic reason is that each ManyToManyField implies the creation of a new table, and there can't be two tables with the same name (i.e. represented by the same class).
One alternative approach (shorter and more DRY) would be this:
class Person(models.Model):
name = models.CharField(max_length=127, blank=False)
to_users = models.ManyToManyField(
'self',
symmetrical=False,
related_name='from_users',
through='Event',
through_fields=('from_user', 'to_user'),
)
class Event(models.Model):
item = models.ForeignKey(Item, related_name='events')
from_user = models.ForeignKey(Person, related_name='events_as_giver')
to_user = models.ForeignKey(Person, related_name='events_as_receiver')
The table structure is the same but the descriptors are different. Accessing related people is a bit easier but accessing related items is a bit harder (for example, instead of person.out_items.all() you would say Item.objects.filter(events__from_user=person).distinct()).
Related
I need to reference Category and SubCategory models in the Product model. But SubCategory has a ForeignKey to Category. Right now I am referencing as shown below. Is there a better way or is this correct?
class Category(models.Model):
category_name = models.CharField(max_length=250)
def __str__(self):
return f'{self.category_name}'
class SubCategory(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
subcategory_name = models.CharField(max_length=250)
def __str__(self):
return f'{self.subcategory_name}'
class Product(models.Model):
company_category = models.ForeignKey(Category, models.SET_NULL, blank=True, null=True)
company_subcategory = models.ForeignKey(SubCategory, models.SET_NULL, blank=True, null=True)
it depends, can product.company_category be different than product.company_subcategory.category, if the answer is yes then your current setup is correct.
But if those two values will always be the same and you want to keep your tables as normalized as possible, then it might be a good idea to only use company_subcategory.
One caveat to the normalized approach is every time you load an instance of Product and you want to reach product.company_subcategory.category you will generate extra sql queries unless you use the select_related or prefetch_related methods
Product.objects.all().prefetch_related('company_subcategory', 'company_subcategory__category')
Product.objects.select_related('company_subcategory', 'company_subcategory__category').get(pk=1)
I have a Django model for a player of a game
class Player(models.Model):
name = models.CharField(max_length=50)
team = models.ForeignKey('Team', on_delete=models.CASCADE, blank=True, null=True)
game = models.ForeignKey('Game', on_delete=models.CASCADE)
objects = GameManager()
class Meta:
unique_together = ('name', 'game',)
I have only one unique constraint, that the name and the game are unique together.
Now, I would like to extend our page by adding registered users. So, I would add this to the model.
user = models.ForeignKey('auth.User', on_delete=models.CASCADE, blank=True, null=True)
So, an registered user can subscribe to a game by adding a name, team, game, and his/her user. However, the user should only be able to add his account once to an game, which would be a second unique constrain
unique_together = ('user', 'game',)
Is it possible to give in Django two unique constraints to the model? Or do I have to search in the table manually prior to saving the new entries? Or is there a better way?
Yes, in fact by default unique_together is a collection of collections of fields that are unique together, so something like:
class Player(models.Model):
name = models.CharField(max_length=50)
team = models.ForeignKey('Team', on_delete=models.CASCADE, blank=True, null=True)
game = models.ForeignKey('Game', on_delete=models.CASCADE)
objects = GameManager()
class Meta:
unique_together = (('name', 'game',), ('user', 'game',))
Here we thus specify that every name, game pair is unique, and every user, game pair is unique. So it is impossible to create two Player objects for the same user and game, or for the same game and name.
It is only because a single unique_together constraint is quite common, that one can also pass a single collection of field names that should be unique together, as is written in the documentation on Options.unique_together [Django-doc]:
Sets of field names that, taken together, must be unique:
unique_together = (("driver", "restaurant"),)
This is a tuple of tuples that must be unique when considered
together. It's used in the Django admin and is enforced at the
database level (i.e., the appropriate UNIQUE statements are included
in the CREATE TABLE statement).
For convenience, unique_together can be a single tuple when dealing with a single set of fields:
unique_together = ("driver", "restaurant")
You should use models.UniqueConstraint (reference).
As noted in the reference:
UniqueConstraint provides more functionality than unique_together. unique_together may be deprecated in the future.
Do this:
class Meta:
constraints = [
models.UniqueConstraint(fields=['name', 'game'], name="unique_name_game"),
models.UniqueConstraint(fields=['user', 'game'], name="unique_user_game"),
]
For example please refer to this :-
class Stores(models.Model):
name = models.CharField(max_length=50)
address = models.CharField(max_length=50)
lat = models.FloatField()
lng = models.FloatField()
merchant = models.ForeignKey(Profile, on_delete=models.CASCADE, related_name="stores")
def __str__(self):
return "{}: {}".format(self.name, self.address)
class Meta:
verbose_name_plural = 'Stores'
class Items(models.Model):
name = models.CharField(max_length=50, unique=False)
price = models.IntegerField()
description = models.TextField()
stores = models.ForeignKey(Stores, on_delete=models.CASCADE, related_name="items")
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "Items"
unique_together = ('name', 'stores',)
When creating many to many relationships we use an intermediary table. Lets say I use the following entities video, category, tag, and VideoCategory, VideoTag to create the relations.
I'm assuming that many tags/categories can have many videos and vice-versa.
And I do it with through keyword 'cause I want to be able to use extra fields in the future if I want.
class Category(models.Model):
category = models.CharField(max_length=50)
def __str__(self):
return self.category
class Tag(models.Model):
tag = models.CharField(max_length=50)
def __str__(self):
return self.tag
class Video(models.Model):
title = models.CharField(max_length=255)
categories = models.ManyToManyField(Category, through='VideoCategory')
tags = models.ManyToManyField(Tag, through='VideoTag')
def __str__(self):
return self.title
class VideoCategory(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
video = models.ForeignKey(Video, on_delete=models.CASCADE)
class VideoTag(models.Model):
tag = models.ForeignKey(Tag, on_delete=models.CASCADE)
video = models.ForeignKey(Video, on_delete=models.CASCADE)
But I was wondering if would be possible to create a taxonomy entity and handle the relationships with categories and tags from just one place.
class Category(models.Model):
category = models.CharField(max_length=50)
def __str__(self):
return self.category
class Tag(models.Model):
tag = models.CharField(max_length=50)
def __str__(self):
return self.tag
class Video(models.Model):
title = models.CharField(max_length=255)
categories = models.ManyToManyField(Category, through='Taxonomy')
tags = models.ManyToManyField(Tag, through='Taxonomy')
def __str__(self):
return self.title
class Taxonomy(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE, null=True)
tag = models.ForeignKey(Tag, on_delete=models.CASCADE, null=True)
video = models.ForeignKey(Video, on_delete=models.CASCADE)
Now the taxonomy entity would hold the category and tag related to videos and vice-versa.
I've included 'null=True' to be able to create relations with categories without tags and with tags but without categories.
If I don't use it. I receive an error:
# sqlite3.IntegrityError: NOT NULL constraint failed: MyApp_taxonomy.category_id
This also means that using that single taxonomy entity for the two relationships could have many NULL Fields if one of these category or tag fields are empty on every concrete relation instance (row).
Question:
What would be better ? To keep the intermediary tables separate (VideoCategory & VideoTag) Or to join these intermediary tables into just one ? (Taxonomy)
Due to my lack of experience with databases I couldn't say if I'm missing something important. If doing it with just one intermediary table would give problems in near future or something like that... Of if it is just fine.
You have to use through_fields argument (doc):
class Video(models.Model):
title = models.CharField(max_length=255)
categories = models.ManyToManyField(Category, through='Taxonomy', through_fields=('video', 'category'))
tags = models.ManyToManyField(Tag, through='Taxonomy', through_fields=('video', 'tag'))
I have two models in my django application, event/user, that have a many-to-many relationship.
Event can have many attendees(user).
User can attend many events.
Django automatically made a bridge table for for this relationship.
My question is how can I add new fields to this table using migrations as there is not a model for this Bridge table.
This can be done by creating a "through" table and adding the fields on it. Check out the docs at https://docs.djangoproject.com/es/1.10/topics/db/models/
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(Person, through='Membership')
def __str__(self): # __unicode__ on Python 2
return self.name
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)
This bridge table is called association table. Imo it is bad practice to edit this table that way. Connecting models by many to many field suggest that there is no model between. If your association table is supposed to represent additional data then you should create new model
class NewModel(models.Model):
attender = models.ForeignKey(User, null=False)
event = models.ForeignKey(Event, null=False)
# additional fileds
and remove many to many relationship from event
Having the error msg
order = models.ForeignKey(Order, on_delete=models.CASCADE)
NameError: name 'Order' is not defined
So how ever I do one class will be missing cuz of the class is below the currently reading class so that the class is missing. How do I solve this? i've read about many-to-many function, might that solve the problem?
class Items(models.Model):
name = models.CharField(max_length=10)
def __str__(self):
return self.name
class OrderedItem(models.Model):
items = models.ForeignKey(Items, on_delete=models.CASCADE)
order = models.ForeignKey(Order, on_delete=models.CASCADE)
amount = models.IntegerField()
def __str__(self):
return self.items
class Order(models.Model):
#clientID
orderedItem = models.ForeignKey(OrderedItem, on_delete=models.CASCADE)
#truckId Foreign key till truck
created = models.DateTimeField(auto_now=False, auto_now_add=True)
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
emergency = models.BooleanField(default=False)
status = models.IntegerField()
#building
#floor
def __str__(self):
return self.id
Use the fully-qualified model string
When this happens, I usually resort to what's called the fully-qualified model string, fancy term for what's essential a string representing the model and the containing app in the format of 'app_label.ModelName'
E.g. if your model is Order, then the model string name would be the string 'Order'
So you can already do:
order = models.ForeignKey('Order', on_delete=models.CASCADE)
With the above, Django will look for the model 'Order' in the same app. It's okay if you have not defined it yet, as long as it is defined.
If that model happens to come from a different app, it would be:
order = models.ForeignKey('appname.Order', on_delete=models.CASCADE)
Reverse query clashes
Because Order points to OrderItems and OrderItems point to Order you get a clash with related queries that Django generate for you. You can disable those with related_name='+':
order = models.ForeignKey('Order', on_delete=models.CASCADE, related_name='+')
Better modeling
Since a OrderedItem already "belongs" to an Order, there's no point in having a ForeignKey from Order to OrderedItem, you can just remove it instead of dealing with the above clash.
So what you'd have would look like this
Item
Order
OrderedItem
+ FK(Item)
+ FK(Order)
A design which wouldn't involve referencing a model that hasn't been defined yet :)
The reason it can't find the class order is because it hasn't been defined yet, you either need to specify it as a string as shown by Shang Wang, or change the order of them in your models.py
class Order(models.Model):
#clientID
orderedItem = models.ForeignKey(OrderedItem, on_delete=models.CASCADE)
#truckId Foreign key till truck
created = models.DateTimeField(auto_now=False, auto_now_add=True)
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
emergency = models.BooleanField(default=False)
status = models.IntegerField()
#building
#floor
def __str__(self):
return self.id
class OrderedItem(models.Model):
items = models.ForeignKey(Items, on_delete=models.CASCADE)
order = models.ForeignKey(Order, on_delete=models.CASCADE)
amount = models.IntegerField()
def __str__(self):
return self.items
Changing the order has its advantages over specifying as a string since it will allow IDE's to find usages of the class should it ever need refactoring.
Since you have foreign keys to both classes, the above won't work and only applies to one-to-one or one-to-many relationships. Instead, it would be better to define a ManyToManyField instead of the two foreign keys
You have defined class Items before use it and you have not any error.
you must define class Order before class OrderedItem.