简体   繁体   English

IntegrityError: NOT NULL 约束失败,未识别外键

[英]IntegrityError: NOT NULL constraint failed, not identifying foreignkey

These are my models.这些是我的模型。

class Category(models.Model):
    name = models.CharField(max_length=255, primary_key=True)
    description = models.TextField(null=True)

    def __str__(self):
        return self.name

class SubCategory(models.Model):
    name = models.CharField(max_length=255, primary_key=True)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    description = models.TextField(null=True)

    def __str__(self):
        return self.name

def product_display_image_path(instance, filename):
    return f"products/{instance.pid}/{filename}/"

def product_image_path(instance, filename):
    return f"products/{instance.product.pid}/{filename}/"

class Product(models.Model):
    pid = models.UUIDField(default=uuid.uuid4, primary_key=True)
    name = models.TextField()
    description = models.TextField()
    display_image = models.ImageField(
        upload_to=product_display_image_path,
        default='products/image_unavailable.png'
    )
    artist = models.ForeignKey(User, on_delete=models.CASCADE)
    category = models.ForeignKey(Category, on_delete=models.PROTECT)
    subcategory = models.ForeignKey(SubCategory, on_delete=models.PROTECT)
    stock_left = models.IntegerField(default=0)
    price = models.CharField(max_length=255)
    click_count = models.BigIntegerField(default=0)
    purchase_count = models.BigIntegerField(default=0)

    def __str__(self):
        return self.name

    class Meta:
        ordering = ['-click_count']

This is my view.这是我的看法。

class ProductCreateView(CreateAPIView):
    queryset = Product.objects.all()
    permission_classes = [IsAuthenticated, IsArtist]
    serializer_class = ProductSerializer
    parser_classes = [FormParser, MultiPartParser, JSONParser]

    def create(self, request, *args, **kwargs):
        request.data['artist'] = request.user.id
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)

This is the request I'm making:这是我提出的要求: 邮递员请求 This is the error:这是错误: 完整性错误 I'm constantly getting this error IntegrityError at /store/create/product/ NOT NULL constraint failed: products_product.category_id (the name of my app is products).IntegrityError at /store/create/product/ NOT NULL constraint failed: products_product.category_id (我的应用程序的名称是 products)。 I have flushed the whole database and tried again.我已经刷新了整个数据库并再次尝试。 Nothing seems to work.似乎没有任何效果。 PS. PS。 I already have a category names 'accessories' and a subcategory named 'bands' created.我已经创建了一个名为“附件”的类别和一个名为“乐队”的子类别。 I dont know why the new product that I want to create is unable to pick the category up.我不知道为什么我要创建的新产品无法选择类别。 Can somebody please help me out with this?有人可以帮我解决这个问题吗?

I missed those fields in the serializer.我错过了序列化程序中的那些字段。 Answered by @Syntactic Fructose in the comments.由@Syntactic Fructose 在评论中回答。 Thank you, community.谢谢你,社区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM