0
이것은 내 models.py, 브랜드 코드를 만들려면 저장 함수를 호출하고 하위 범주의 부모 범주를 검사하는 또한 원래 부모 범주에 속해 있어야합니다. 다른 카테고리. 제발 도와주세요.clean()을 호출해도 필수 필드가 점검되지 않고 관리자 패널에 오류가 발생합니다. 그러나 그 깨끗한() 제거 할 때 필요한 필드의 유효성을 검사합니다
class Brand(models.Model):
category = models.ForeignKey(Category, related_name='category', limit_choices_to={'parent_category__isnull': True})
sub_category = models.ForeignKey(Category, related_name='sub_category', limit_choices_to={'parent_category__isnull': False})
brand_code = models.CharField(max_length=70, null=True, blank=True)
brand_name = models.CharField(max_length=255)
def __str__(self):
return self.brand_name
def create_brand_code(self):
pass
def clean(self):
if not self.sub_category.parent_category == self.category:
raise ValidationError("{} is not the sub category of {}".format(self.sub_category, self.category))
def save(self, *args, **kwargs):
if not self.brand_code:
if self.sub_category.parent_category == self.category:
self.brand_code = self.create_brand_code()
super(Brand, self).save(*args, **kwargs)