Associate addditional value to a manytomanyfield


Associate addditional value to a manytomanyfield



I have 2 models book and favorite


class Book(models.Model):
title = models.CharField(max_length=200)

class Favorite(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
favorites = models.ManyToManyField(Book, related_name='favorited_by')



Here a user has its own sets of favorite books. I want to associate an additional variable to each selected favorite book but how can this be done?





By specifying a through model...
– Willem Van Onsem
Jul 2 at 19:27


through





But your modeling is actually quite strange to begin with, you use a OneToOne to a user?
– Willem Van Onsem
Jul 2 at 19:29


OneToOne





what is the other way? I'm new to django.
– Ahmed
Jul 2 at 19:34




1 Answer
1



You can define a through model: a model that is used to write many-to-many relations. But your modeling in general is a bit strange. Why do you use a OneToOneField to a User here? Isn't a Favorite a single favorite (thus the fact that a single user likes a single book:


through


OneToOneField


User


Favorite


from django.conf import settings

class Book(models.Model):
title = models.CharField(max_length=200)
users = models.ManyToManyField(settings.AUTH_USER_MODEL, through='Favorite')

class Favorite(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
book = models.ForeignKey(Book, on_delete=models.CASCADE)
additional = models.IntegerField()



We thus model it as a three models:


+-------+ 1 M +------------+ N 1 +------+
| Book |-------| Favorite |--------| User |
+-------+ +------------+ +------+
| title | | additional |
+-------+ +------------+



Now Favorite is thus seen as the fact that a User liked a Book. Not as one-to-one model instance attached to a User that will contain all favorites.


Favorite


User


Book


User



For more, see the documentation on the through parameter [Django-doc].


through





I am implementing in a way that user can select multiple favorites
– Ahmed
Jul 2 at 19:47





@Ahmed: well that is here perfectly possible, since every favorite a user has, is a single Favorite instance for that user. It is rather weird that you use a model called Favorite to store *multiple Favorites in. In your implementation, the Favorite has not much added value, since your Favorite basically acts like a proxy object to a User.
– Willem Van Onsem
Jul 2 at 19:49



Favorite


Favorite


Favorite


Favorite


Favorite


User





Typically using such shallow models with only a OneToOne field are not good since (a) they create tables that only add data duplication (which is already an anti-pattern), (b) it will make querying more complex, and (c) and it is less efficient.
– Willem Van Onsem
Jul 2 at 19:52


OneToOne





Oh yes it is working Thankyou.
– Ahmed
Jul 2 at 19:54






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages