特定用户内容添加,编辑,删除特定用户的特定数据

时间:2016-03-15 23:25:19

标签: python django django-models django-admin

我开始组织一个包含几个模型的项目,但有两个主要模型是品牌和产品。

我将允许客户登录Django管理站点以编辑他们的品牌,以及添加,编辑和删除他们自己的产品。

然而,当编辑他们的产品时,所述客户端将只能添加,编辑&删除自己的产品;因此,例如,由于权限限制,他们的品牌名称已经在外键下拉列表中被选中。从那里,他们可以重复添加品牌,删除和编辑品牌的过程。

当前设置&问题

目前我按照我想要的方式设置了Brands,只是让客户只看到他们要编辑的品牌。但对于产品,他们可以看到所有产品,即使是不同的品牌(这不是我想要的);当他们创造一种新产品时,他们必须从下拉产品中进行选择以匹配(这不是我想要的)。我现在使用默认的Django权限,使用一些ModelAdmin方法。

models.py

class TimeStampedModel(models.Model):
    """
    An abstract base class model that provides self updating
    ``created`` and ``modified`` fields.
    """

    created = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    modified = models.DateTimeField(auto_now=True, blank=True, null=True)

    # Metadata
    class Meta:
        abstract = True


class Brand(TimeStampedModel):
    """
    Information for each brand
    """

    # Primary Key
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

    # General information
    brand_name = models.CharField(max_length=100, blank=True, default="", unique=True)
    brand_description = models.TextField(max_length=100, blank=True, default="")
    brand_origin_city = models.CharField(max_length=100, blank=True, default="")
    brand_origin_state_or_country = models.CharField(max_length=100, blank=True, default="",
        help_text=_('State (USA only), Country (International only)'), verbose_name=_('State or Country'))
    brand_feature_image = CloudinaryField('Featured Brand Image', null=True, blank=True)
    brand_isActive = models.BooleanField(default=False, verbose_name=_('Brand active'))

    # Metadata
    class Meta: 
        verbose_name = _('Brand')
        verbose_name_plural = _('Brands')

    def __unicode__(self):
        return "{0}".format( self.brand_name )

class Product(TimeStampedModel):
    """
    Product for each brand
    """

    product_name = models.CharField(max_length=100, blank=True, default="", verbose_name=_('Product Name') )
    product_url = models.URLField(max_length=100, blank=True,  default="",
        help_text=_('This is for the product web url to the particular item on your website.'), verbose_name=_('Product Url'))
    product_price = CurrencyField( verbose_name=_('Product price') )
    product_image = CloudinaryField('Product Image', blank=True,  null=True)

    # Foreign Key
    brand = models.ForeignKey('Brand', null=True, to_field="id")

    #Metadata
    class Meta: 
        verbose_name = _('Product')
        verbose_name_plural = _('Products')

    def __unicode__(self):
        return "{0}".format( self.product_name )

Admin.py

class BrandAdmin(admin.ModelAdmin):
    list_display = ["brand_name", "brand_origin_city", "brand_origin_state_or_country"]
    search_fields = ["brand_name"]
    list_per_page = 10  

    def save_model(self, request, obj, form, change):
        obj.user = request.user
        super(BrandAdmin, self).save_model(request, obj, form, change)

        obj.save()

    def get_queryset(self, request):
        qs = super(BrandAdmin, self).get_queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter(brand_name=request.user)

class ProductAdmin(admin.ModelAdmin):
    list_display = ["product_name","product_price"]
    search_fields = ["product_name"]
    list_per_page = 20  

    def save_model(self, request, obj, form, change):
        obj.user = request.user

        super(ProductAdmin, self).save_model(request, obj, form, change)

        obj.save()

    def get_queryset(self, request):
        qs = super(ProductAdmin, self).get_queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter(brand=request.user)


# Register Models below
admin.site.register(Brand, BrandAdmin)
admin.site.register(Product, ProductAdmin)

解决这个问题的解决方案是什么?

1 个答案:

答案 0 :(得分:1)

class ProductAdmin(admin.ModelAdmin):
    ...
    def get_queryset(self, request):
      ...
      return qs.filter(brand__brand_name = request.user)

希望这项工作。如果没有,请告诉我。