> > I only intend to show products added by the shop owner. > You can do this by using the reverse m2m relation between product and shop.
products = Product.objects.filter(shop__shop_user=request.user) However, I think it's more suitable for the product to keep the M2M relation, because naturally when you add a new product you would want to choose its shops. If a product can belong to only one shop, then you can even use a Foreign Key instead of M2M Field. Also I need to register shop using built-in auth system so how do I do that > I'm not sure what is the question here, you have access to request.user, just assign it to shop_user when you create a new shop. should I add user field in Shop or in Product. > It really depends on what you need, you already mention that you need a Shop.owner, so you can't go without user FK into Shop model. IF you think you are gonna need and a Product owner/creator as well, then you can add user FK to products as well. These things are not mutually exclusive. On Wednesday, August 17, 2016 at 4:37:51 PM UTC+3, Shamaila Moazzam wrote: > > I am trying to create a parent model for my products app and I need to > provide the user reference. I've registered the Shop model with admin.py. > Now I have problem that when I see in model it shows all the products > listed before. I only intend to show products added by the shop owner. Also > I need to register shop using built-in auth system so how do I do that, > should I add user field in Shop or in Product. To brief in detail following > are my models. Thanks for your kind help and apologies if as beginner i've > missed any information or if the question isn't up to standard. > > shops/models.py: > > class Shop(models.Model): > #shop_user = models.ForeignKey(settings.USER_AUTH_MODEL, blank=False, > null=False) > product = models.ManyToManyField(Product, related_name='myshop',null= > False, blank=True) > Title = models.CharField(max_length=120, null=False) > image = models.ImageField(upload_to=image_upload_to_shop, null=True) > location = models.CharField(max_length=120) > > > > > def __unicode__(self): > return str(self.Title) > > > products/models.py: > > class Product(models.Model): > #user = models.ForeignKey(settings.USER_AUTH_MODEL) > title = models.CharField(max_length=120) > description = models.TextField(blank=True, null=True) > price = models.DecimalField(decimal_places=2, max_digits=20) > publish_date = models.DateTimeField(auto_now=False, auto_now_add=False > , null=True, blank= True) > expire_date = models.DateTimeField(auto_now=False, auto_now_add=False, > null=True, blank=True) > active = models.BooleanField(default=True) > categories = models.ManyToManyField('Category', blank=True) > default = models.ForeignKey('Category', related_name= > 'default_category', null=True, blank=True) > hitcounts = GenericRelation(HitCount, content_type_field= > 'content_type', object_id_field='object_pk',) > > > def __unicode__(self): #def __str__(self): > return self.title > > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1a565b81-f173-43a9-aa6c-63f3b51193e9%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.