Note that there are some explanatory texts on larger screens.

plurals
  1. POError while adding objects via admin site in django
    primarykey
    data
    text
    <p>I've created the following models:</p> <pre><code>class BasePrice(models.Model): start_hour = models.TimeField(blank=False) end_hour = models.TimeField(blank=False) monday = models.BooleanField(blank=False) tuesday = models.BooleanField(blank=False) wednesday = models.BooleanField(blank=False) thursday = models.BooleanField(blank=False) friday = models.BooleanField(blank=False) price = models.IntegerField(blank=False) def __unicode__(self): days = "" if (self.monday == True): days = days + " Mon" if (self.tuesday == True): days = days + " Tue" if (self.wednesday == True): days = days + " Wed" if (self.thursday == True): days = days + " Thu" if (self.friday == True): days = days + " Fri" return "Price " + str(self.price) + " at " + str(self.start_hour) + ":" +str(self.end_hour) + " in " + days def clean(self): if self.start_hour &gt; self.end_hour: raise ValidationError('Start hour is older than end hour!') class RentPeriod(models.Model): start_date = models.DateField(blank=False) end_date = models.DateField(blank=False) desk = models.ForeignKey(Desk) base_prices = models.ManyToManyField(BasePrice) def __unicode__(self): return "Period " + str(self.start_date) + " to " + str(self.end_date) + " for " + self.desk.__unicode__() def clean(self): if self.start_date &gt; self.end_date: raise ValidationError('Start date is older than end date!') def validate_unique(self, *args, **kwargs): super(RentPeriod, self).validate_unique(*args, **kwargs) # overlaping hours basePrices = self.base_prices.all() for price in basePrices: qs = self.__class__._default_manager.filter( (Q(monday=True) &amp; Q(monday=price.monday)) | (Q(tuesday=True) &amp; Q(tuesday=price.tuesday)) | (Q(wednesday=True) &amp; Q(wednesday=price.wednesday)) | (Q(thursday=True) &amp; Q(thursday=price.thursday)) | (Q(friday=True) &amp; Q(friday=price.friday)), start_hour__lte=self.end_hour, end_hour__gte=self.start_hour ) if qs.exists(): raise ValidationError({NON_FIELD_ERRORS: ('overlaping hours range',)}) </code></pre> <p>Generally, I'd like to avoid overlaping time in specified days when adding instances of RentPeriod via <strong>admin site.</strong> When I try to add RentPeriod instance, I get following error:</p> <blockquote> <p>'' needs to have a value for field "rentperiod" before this many-to-many relationship can be used.</p> </blockquote> <p>I've read <a href="https://stackoverflow.com/questions/6090859/django-instance-needs-to-have-a-primary-key-value-before-a-many-to-many-relatio">click</a>, but I don't have idea how to get it to work. Could you help?</p> <p><strong>Attention:</strong> complexity (n^2) has no matter in this case.</p> <p><strong>UPDATE</strong> I've created custom validator according to <a href="https://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-custom-validation-to-the-admin" rel="nofollow noreferrer">documentation</a></p> <p>forms.py</p> <pre><code>from biurrko.rents.models import RentPeriod from django.forms.models import ModelForm class RentPeriodForm(ModelForm): class Meta: model = RentPeriod def clean(self): cleaned_data = self.cleaned_data basePrices = cleaned_data['base_prices'] end_hour = cleaned_data['end_hour'] start_hour = cleaned_data['start_hour'] for price in basePrices: qs = basePrices.filter( (Q(monday=True) &amp; Q(monday=price.monday)) | (Q(tuesday=True) &amp; Q(tuesday=price.tuesday)) | (Q(wednesday=True) &amp; Q(wednesday=price.wednesday)) | (Q(thursday=True) &amp; Q(thursday=price.thursday)) | (Q(friday=True) &amp; Q(friday=price.friday)), start_hour__lte=end_hour, end_hour__gte=start_hour ) if qs.exists(): raise ValidationError({NON_FIELD_ERRORS: ('overlaping hours range',)}) # only for test raise ValidationError({NON_FIELD_ERRORS: ('reached',)}) # return cleaned_data </code></pre> <p>and admin.py</p> <pre><code>from django.contrib import admin from biurrko.rents.models import Desk, Room, RentPeriod, BasePrice from biurrko.rents.forms import RentPeriodForm admin.site.register(Desk) admin.site.register(Room) admin.site.register(RentPeriod) admin.site.register(BasePrice) class RentPeriodAdmin(admin.ModelAdmin): form = RentPeriodForm </code></pre> <p>Unfortunately, own validator "is not called" - I mean even test ValidationError is not raising.</p> <p><strong><em>UPDATE 2</em></strong> Figuret out that it was problem with "register" statement. It should be:</p> <pre><code>admin.site.register(RentPeriod, RentPeriodAdmin) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload