AdminInline和未知的IntegrityError UNIQUE约束失败

 收藏
Django Version: 3.0.4
Exception Type: IntegrityError
Exception Value:    
UNIQUE constraint failed: store_hoursofoperation.day

Hello, I'm trying to resolve this error, but I don't know how to proceed. I want the Store model to contain multiple HoursOfOperation. However, this error occurs whenever I add another weekday on any store -- Store A has Monday hours, Store B will error if I add Monday hours.

I am using the default admin to include the HoursOfOperation within the Store model.

如何阻止HoursOfOperation.day具有唯一性?

image-of-admin-panel

admin.site.register(HoursOfOperation)


class HoursOfOperationInline(admin.TabularInline):
    model = HoursOfOperation
    extra = 0

@admin.register(Store)
class StoreAdmin(admin.ModelAdmin):
    inlines = [
        HoursOfOperationInline
    ]
WEEKDAYS = [
    (1, ("Monday")),
    (2, ("Tuesday")),
    (3, ("Wednesday")),
    (4, ("Thursday")),
    (5, ("Friday")),
    (6, ("Saturday")),
    (7, ("Sunday")),
]


class Store(models.Model):
    name = models.CharField(max_length=250)
    summary = models.CharField(max_length=250)
    address_line1 = models.CharField(max_length=100)
    address_line2 = models.CharField(max_length=50)
    phone = models.CharField(max_length=12)
    website = models.CharField(max_length=50)
    status_storefront = models.BooleanField(
        help_text="Is the storefront open to customers?"
    )
    status_takeout = models.BooleanField(
        help_text="Is takeout available for customers?"
    )
    status_delivery = models.BooleanField(
        help_text="What is the delivery service? [No, Yes, Custom]"
    )
    status_delivery_service = models.CharField(
        max_length=20,
        blank=True,
        null=True
    )
    anchor_id = models.CharField(max_length=30)
    hours = models.Many

    @property
    def storefront(self):
        return "Yes" if self.status_storefront else "No"

    @property
    def takeout(self):
        return "Yes" if self.status_takeout else "No"

    @property
    def delivery(self):
        if self.status_delivery:
            if self.status_delivery_service:
                return self.status_delivery_service
            return "Yes"
        else:
            return "No"



class HoursOfOperation(models.Model):
    store = models.ForeignKey(Store, on_delete=models.CASCADE)
    day = models.IntegerField(choices=WEEKDAYS)
    time_open = models.TimeField(
        auto_now=False,
        auto_now_add=False,
        blank=True,
        null=True
    )
    time_close = models.TimeField(
        auto_now=False,
        auto_now_add=False,
        blank=True,
        null=True
    )   

    def get_day_display(self):
        return WEEKDAYS[self.day]

    @property
    def open(self):
        if self.time_open and self.time_close:
            return self.time_open
        else:
            return "Closed"

    @property
    def close(self):
        if self.time_open and self.time_close:
            return self.time_close
        else:
            return ""

    class Meta:
        ordering = ('time_open', 'time_close')
回复
  • xnam 回复

    @IainShelvington's comment on running manage.py makemigrations did it.

    Even if you think you've run manage.py makemigrations and manage.py migrate, double check your code and run it one more time before giving up.