0
我開始組織一個項目,但有兩個主要模型是品牌和產品。特定用戶內容添加,編輯,刪除特定用戶的特定數據
我將允許客戶在Django管理站點上登錄以編輯他們的品牌並添加,編輯和刪除他們自己的產品。
但是當編輯他們的產品時,表示客戶端只能夠添加,編輯刪除自己的產品;因此,例如,由於權限限制,他們的品牌名稱將在外鍵下拉菜單中被選中。從那裏他們可以重複添加品牌和刪除和編輯品牌的過程。
當前設置&問題
目前我有這種設置對於品牌我想順便說一下,只是一個客戶端只能看到自己的品牌來編輯。但對於產品,他們可以看到全部的產品,即使是不同的品牌(這不是我想要的);當他們創造新產品時,他們必須從產品的下拉列表中進行選擇以符合(這不是我想要的)。我使用默認的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)
什麼是解決這個問題的解決方案?