Can't make Django ORM work for multiple models

41 views
Skip to first unread message

Shamaila Moazzam

unread,
Aug 17, 2016, 9:37:51 AM8/17/16
to Django users
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


Todor Velichkov

unread,
Aug 17, 2016, 10:52:20 AM8/17/16
to Django users
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.


Shamaila Moazzam

unread,
Aug 17, 2016, 11:35:32 AM8/17/16
to Django users
Thanks for reply Todor, But how to fix it in admin.py?

Shamaila Moazzam

unread,
Aug 17, 2016, 1:11:23 PM8/17/16
to Django users
shops/admin.py

from .models import Shop

admin.site.register(Shop)



products/admin.py


from .models import Product, Variation, ProductImage, Category, ProductFeatured, color_product, size_product



class ProductImageInline(admin.TabularInline):
model = ProductImage
extra = 0
max_num = 10

class VariationInline(admin.TabularInline):
model = Variation
extra = 0
max_num = 10


class ProductAdmin(admin.ModelAdmin):
list_display = ['__unicode__', 'price',]
inlines = [
ProductImageInline,
VariationInline,


]
class Meta:
model = Product


admin.site.register(Product, ProductAdmin)




#admin.site.register(Variation)

admin.site.register(ProductImage)


admin.site.register(Category)


admin.site.register(ProductFeatured)

admin.site.register(color_product)

admin.site.register(size_product)



Thanks for reply Todor, But how to fix it in admin.py?

Todor Velichkov

unread,
Aug 17, 2016, 3:43:26 PM8/17/16
to Django users
I don't know how much you are into Django, but in case you didn't you really need to ready carefully the following articles from the docs.

- Working with forms
- Creating forms from models
- The Django admin site

there is more but this is just to get started.

now after this, you should understand that in order to achieve what you want, you could do something like this:

class ProductAdmin(admin.ModelAdmin):
   
#...
    exclude
= ('shop_user',)

   
def save_model(self, request, obj, form, change):
       
if not change:
           
#set user on create only
            obj
.shop_user = request.user
        obj
.save()
Reply all
Reply to author
Forward
0 new messages