Implementing django auth system with relational models

41 views
Skip to first unread message

Shamaila Moazzam

unread,
Aug 17, 2016, 1:37:51 PM8/17/16
to Django users
Hi,

I am a beginner and please forgive me if my question is not up to the standard. I've got two models one is Product which is saved in db already and another one is Shop. I am trying to related both models with following code.

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',)


    objects
= ProductManager()


   
class Meta:
        ordering
= ["-title"]


   
def __unicode__(self):
       
return self.title


class Shop(models.Model):
    product
= models.ManyToManyField(Product)

    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)




With above I've got it added in my admin.py for Shop app and now I have a problem. When I add a shop it shows all the past products prepopulated in my "products" field. 
I just need to add the products that shop account holder has uploaded. I wish to use django\s built-in auth model to register shop holders. Again the confusion is that where should I add 
USER_AUTH_MODEL in "Shop" or in "Products". If I added it shop app then for beginner like me it will be easy to use querysets. Please advise the best practice for above scenario.

ludovic coues

unread,
Aug 17, 2016, 1:47:48 PM8/17/16
to django...@googlegroups.com
Do you have any error with the code you posted ?
It look like you already have a foreign key to your user.
> --
> 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...@googlegroups.com.
> To post to this group, send email to django...@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/bf649b76-0a5e-4e48-b54c-dfdcd0ae1a16%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

M Hashmi

unread,
Aug 17, 2016, 2:35:28 PM8/17/16
to django...@googlegroups.com
So what you are saying is that you don't need to get all the products instead only products uploaded by a particular user?
This makes sense because you are getting the whole model called with the key so if I am getting your question right you just need to apply some sort of filter to get user related products. Which means by default first time user gets registered it shouldn't show any products but same time if product is added by the shop owner he should be able to see that.

Respond please.


> To post to this group, send email to django...@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/bf649b76-0a5e-4e48-b54c-dfdcd0ae1a16%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42
--
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+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Shamaila Moazzam

unread,
Aug 17, 2016, 2:41:57 PM8/17/16
to Django users

@Mudassar this is exactly what I need.

Shamaila Moazzam

unread,
Aug 17, 2016, 3:20:47 PM8/17/16
to Django users
@Ludovic there is no error. Just I don't want to get all the products pre-populated. I need empty products field and only when user uploads products then it should show me products. Please advise


On Wednesday, August 17, 2016 at 6:37:51 PM UTC+5, Shamaila Moazzam wrote:

ludovic coues

unread,
Aug 17, 2016, 4:23:31 PM8/17/16
to django...@googlegroups.com
Could you share your admin.py file ?

You might be interested into this part of the documentation:
https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_foreignkey
> --
> 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...@googlegroups.com.
> To post to this group, send email to django...@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/7e4ba681-de3c-4cda-9940-3312987e9c06%40googlegroups.com.

Shamaila Moazzam

unread,
Aug 17, 2016, 5:27:16 PM8/17/16
to Django users


On Wednesday, August 17, 2016 at 6:37:51 PM UTC+5, Shamaila Moazzam wrote:

Shamaila Moazzam

unread,
Aug 17, 2016, 5:31:18 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)

ludovic coues

unread,
Aug 17, 2016, 9:38:46 PM8/17/16
to django...@googlegroups.com
Ok, sorry, I made a off by one error on the link :)

Try that:

# shops/admin.py
from django.contrib import admin
from products.models import Product
from .models import Shop

class ShopAdmin(admin.ModelAdmin):
def formfield_for_manytomany(self, db_field, request, **kwargs):
if db_field.name == "product":
kwargs["queryset"] = Product.objects.filter(user=request.user)
return super(ShopAdmin,
self).formfield_for_manytomany(db_field, request, **kwargs)

admin.site.register(Shop, ShopAdmin)

###

It should give you the desired result
> https://groups.google.com/d/msgid/django-users/4470ec74-21bf-48f5-8496-7b4c18184e19%40googlegroups.com.

M Hashmi

unread,
Aug 18, 2016, 10:57:03 AM8/18/16
to django...@googlegroups.com
Ludovic,

Shouldn't she be using model managers for this?

Just curious.

Regards,
Mudassar







> To post to this group, send email to django...@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/4470ec74-21bf-48f5-8496-7b4c18184e19%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

--
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+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

ludovic coues

unread,
Aug 18, 2016, 11:52:38 AM8/18/16
to django...@googlegroups.com
No idea. I've never looked into model managers.
Maybe they can help in non-repeating code, maybe they won't work due
to the lack of access to the currently logged user.
>> > https://groups.google.com/d/msgid/django-users/4470ec74-21bf-48f5-8496-7b4c18184e19%40googlegroups.com.
>> >
>> > For more options, visit https://groups.google.com/d/optout.
>>
>>
>>
>> --
>>
>> Cordialement, Coues Ludovic
>> +336 148 743 42
>>
>> --
>> 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...@googlegroups.com.
>> To post to this group, send email to django...@googlegroups.com.
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit
> --
> 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...@googlegroups.com.
> To post to this group, send email to django...@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/CANoUts6i8m1LZRTivOY%3DFRxT1X2fzDZL4UPw4qXUJViT%2B-9tyw%40mail.gmail.com.

Shamaila Moazzam

unread,
Aug 18, 2016, 2:59:29 PM8/18/16
to Django users
ok thanx @  ludovic coues...
i will try this and then tell you
regards
Reply all
Reply to author
Forward
0 new messages