我正在构建一个停车应用程序,我的问题是我使用了4种不同的约束。
我的问题:如果10月25日我预订的P1地点是9月17日,那么我希望它免费提供18-24岁的预订。
我的模特:
from django.db import models
from django.conf import settings
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _
from datetime import datetime, timedelta, time
from django.core.exceptions import NON_FIELD_ERRORS
today = datetime.now().date()
tomorrow = today + timedelta(1)
now = datetime.now()
l = now.hour
m=int(now.strftime("%H"))
class ParcareManager(models.Manager):
def active(self, *args, **kwargs):
return super(ParcareManager, self).filter(draft=False).filter(parking_on__lte=datetime.now())
class Parcare(models.Model):
PARKING_PLOT = (('P1', 'Parking #1'), ('P2', 'Parking #2'),('P3', 'Parking #3'))
user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True,null=True, default=1, on_delete=True)
email=models.EmailField(blank=True, null=True)
parking_on = models.DateField(auto_now=False, auto_now_add=False,blank=True, null=True,
help_text='Alege data cand doresti sa vii in office',)
parking_off = models.DateField(auto_now=False, auto_now_add=False, blank=True, null=True,
help_text='Alege Data Plecarii')
numar_masina = models.CharField(max_length=8, default="IF77WXV",blank=True, null=True,
help_text='Introdu Numarul Masinii')
location = models.CharField(max_length=3, blank=True, default="P1",null=True, choices=PARKING_PLOT,
help_text='Alege Locul de Parcare Dorit')
updated = models.DateTimeField(auto_now=True, auto_now_add=False,blank=True, null=True)
timestamp=models.DateTimeField(auto_now=False, auto_now_add=True,blank=True, null=True)
venire = models.TimeField(default=time(9, 00), auto_now=False,auto_now_add=False, help_text='Alege Ora Venirii')
plecare = models.TimeField(default=time(18, 00), auto_now=False, auto_now_add=False, help_text='Alege Ora Plecarii')
objects = ParcareManager()
def __str__(self):
return self.location + " | " + str(self.parking_on) + " | " + str(self.parking_off)
def validate_unique(self, exclude=None):
try:
super(Parcare, self).validate_unique()
except ValidationError as e:
raise ValidationError("Dear " + str(self.user) +
" seems that the parking places "+ str(self.location) +
" you want to book, is already taken on "+ str(self.parking_on) + " at"+ str(self.venire)+" !"+
" Please select another date! ")
class Meta:
verbose_name_plural = "parcare"
ordering = ["-parking_on"]
unique_together = ("parking_on", "location", "venire", "plecare")
错误:
IntegrityError at /admin/parcare/parcare/add/
UNIQUE constraint failed: parcare_parcare.parking_on, parcare_parcare.location, parcare_parcare.venire, parcare_parcare.plecare
如果用户选择从9月17日的25日预订,但从18-24日给我这个错误,它就像一个咒语。
管理员:
from django.contrib import admin
from .models import Parcare
from django.core.exceptions import NON_FIELD_ERRORS
class ParcareModelAdmin(admin.ModelAdmin):
list_display = ["user", "location",
"parking_on", "parking_off", "venire", "plecare", "timestamp"]
list_display_links = ["user" , "location"]
list_editable = [ "parking_off", "parking_on","venire", "plecare"]
list_filter = ["parking_on","location", "email"]
search_fields = ["location", "parking_on"]
date_hierarchy='parking_on'
class Meta:
model = Parcare
def get_form(self, request, obj=None, **kwargs):
form = super().get_form(request, obj, **kwargs)
if not obj:
user = request.user
form.base_fields['user'].initial = user
form.base_fields['email'].initial = user.email
return form
admin.site.register(Parcare, ParcareModelAdmin)
仅在unique_together =(“ parking_on”,“位置”)下可以正常工作,但是当他们想出入时,我需要进行小时的验证。
答案 0 :(得分:0)
protected App(Logic logic) {
this(logic.configuration().welcomeScreen, logic.configuration().name, Optional.of(logic));
}