supersaiyajin87
supersaiyajin87

Reputation: 153

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: Postman request This is the error: integrity 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). I have flushed the whole database and tried again. Nothing seems to work. 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?

Upvotes: 0

Views: 243

Answers (1)

supersaiyajin87
supersaiyajin87

Reputation: 153

I missed those fields in the serializer. Answered by @Syntactic Fructose in the comments. Thank you, community.

Upvotes: 1

Related Questions