0

I am making review api with django, but I have a problem.

models.py

from django.db import models
import uuid

# Create your models here.
from django.utils.text import slugify

class buildingData(models.Model):
    building_name = models.CharField(max_length=50, unique=True)
    slug = models.SlugField(unique=True, default=uuid.uuid1)
    building_loc = models.CharField(max_length=50)
    building_call = models.CharField(max_length=20)
    building_time = models.CharField(max_length=50)

    def save(self, *args, **kwargs):
        self.slug = slugify(self.building_name)
        return super().save(*args, **kwargs)
        

class reviewData(models.Model):
    building = models.ForeignKey(buildingData, related_name='reviews', on_delete=models.CASCADE, null=False, blank=False)
    review_content = models.TextField()
    star_num = models.FloatField()

urls.py

from django.contrib import admin
from django.urls import path
from crawling_data.views import ReviewListAPI
from crawling_data.views import BuildingInfoAPI

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/buildingdata/', BuildingInfoAPI.as_view()),
    path('api/buildingdata/<slug:slug>/', ReviewListAPI.as_view())
]

I am collecting data with crawling, but...

django.db.utils.IntegrityError: UNIQUE constraint failed: crawling_data_buildingdata.slug

This error occurs.

I've tried deleting migrate file and migration again, but still doesn't work.

Is there any problem on my code or is there other way to solve this?

2
  • When does this error occur? When you try to migrate to the database? Commented Oct 11, 2021 at 14:40
  • It occurs when I save data that I collected with crawling program @bichanna Commented Oct 11, 2021 at 14:43

1 Answer 1

1

That is because the uuid1 is generated from your machine ID and time stamp and the machine ID in your case remains constant, that's why they look pretty similar at the end. You may use uuid4() to get a random unique UUID.

Answer from @ZdaR in this answer

You should change the first model to this:

class buildingData(models.Model):
    building_name = models.CharField(max_length=50, unique=True)
    slug = models.SlugField(unique=True, default=uuid.uuid4)
    building_loc = models.CharField(max_length=50)
    building_call = models.CharField(max_length=20)
    building_time = models.CharField(max_length=50)

Also you can use your slug as primary key with this:

slug = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

Edited:

I see your save function. in your save function you should remove the slug assignment. Or change it to self.slug = None. If you want to have a slug from the building name you must use TextField or CharField in the model:

slug = models.TextField(unique=True, default=uuid.uuid4)
Sign up to request clarification or add additional context in comments.

6 Comments

django.core.exceptions.ValidationError: ['“” is not a valid UUID.'] This error occurs. How to fix it?
dear @T1JYH, I think you assign an empty string to slug when you create it. item.slug = "" is wrong. you should leave it or using None. item.slug = None is correct.
I've tried first thing instead, but uuid4 still occurs same error with uuid1. Hmm... Those two solutions occur different errors
I edit my answer!
Uh still didn't fixed... I should try tomorrow. Well I must use building name for review api url.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.