Now that Django supports the DateRangeField, is there a 'Pythonic' way to prevent records from having overlapping date ranges?
Hypothetical use case
One hypothetical use case would be a booking system, where you don't want people to book the same resource at the same time.
Hypothetical example code
class Booking(models.model):
# The resource to be reserved
resource = models.ForeignKey('Resource')
# When to reserve the resource
date_range = models.DateRangeField()
class Meta:
unique_together = ('resource', 'date_range',)
I know that the answer is old, but now you can just create a constraint in the meta of the model, that will make Postgres handle this
from django.contrib.postgres.constraints import ExclusionConstraint
from django.contrib.postgres.fields import DateTimeRangeField, RangeOperators
from django.db import models
from django.db.models import Q
class Room(models.Model):
number = models.IntegerField()
class Reservation(models.Model):
room = models.ForeignKey('Room', on_delete=models.CASCADE)
timespan = DateTimeRangeField()
cancelled = models.BooleanField(default=False)
class Meta:
constraints = [
ExclusionConstraint(
name='exclude_overlapping_reservations',
expressions=[
('timespan', RangeOperators.OVERLAPS),
('room', RangeOperators.EQUAL),
],
condition=Q(cancelled=False),
),
]
Postgress Coonstraints
You can check this in your model full_clean method, which is called automatically during ModelForm validation. It is NOT called automatically if you directly save the object.. this is a known problem with Django validation that you may be aware of already! So if you want validation any time the object is saved, you have to also override the model save method.
class Booking(models.model):
def full_clean(self, *args, **kwargs):
super(Booking, self).full_clean(*args, **kwargs)
o = Booking.objects.filter(date_range__overlap=self.date_range).exclude(pk=self.pk).first()
if o:
raise forms.ValidationError('Date Range overlaps with "%s"' % o)
# do not need to do this if you are only saving the object via a ModelForm, since the ModelForm calls FullClean.
def save(self):
self.full_clean()
super(Booking, self).save()
Related
I have a route where meta-data can be POSTed. If known fields are POSTed, I would like to store them in a structured manner in my DB, only storing unknown fields or fields that fail validation in a JSONField.
Let's assume my model to be:
# models.py
from django.db import models
class MetaData(models.Model):
shipping_address_zip_code = models.CharField(max_length=5, blank=True, null=True)
...
unparseable_info = models.JSONField(blank=True, null=True)
I would like to use the built-in serialisation logic to validate whether a zip_code is valid (5 letters or less). If it is, I would proceed normally and store it in the shipping_address_zip_code field. If it fails validation however, I would like to store it as a key-value-pair in the unparseable_info field and still return a success message to the client calling the route.
I have many more fields and am looking for a generic solution, but only including one field here probably helps in illustrating my problem.
As you are looking for a generic solution, there are a few points that you should consider:
Make sure not to place any model-level validations in your model as you want it to get saved irrespective of the validation status.
Only validate on the serializer-level with custom validation methods.
Make unparseable_info field read-only as it is something we don't want the user to send but receive.
Make use of the errors dictionary provided by the serializer as it gets populated with field-specific errors when we call is_valid.
This is how it might translate into code, inside models.py:
class MetaData(models.Model):
shipping_address_zip_code = models.CharField(blank=True, null=True)
...
unparseable_info = models.JSONField(blank=True, null=True)
then inside serializers.py:
class MetaDataSerializer(serializers.ModelSerializer):
class Meta:
model = MetaData
read_only_fields = ('unparseable_info', )
fields = '__all__'
# Write validators for all of your fields.
finally inside your views.py method, something like this (you can do this inside serializer's save method as well):
meta_data = MetaDataSerializer(data=request.data)
if not meta_data.is_valid():
meta_data.unparseable_info = meta_data.errors
meta_data.save()
# Return meta_data.data in JSONResponse.
You can use Django serializer that store fields that fail validation in JSONField.
Here is an example that worked for me:
from rest_framework import serializers
class MetaDataSerializer(serializers.ModelSerializer):
class Meta:
model = MetaData
fields = 'all'
def validate_shipping_address_zip_code(self, value):
if len(value) > 5:
raise serializers.ValidationError("Zip code must be 5 characters or less.")
return value
def create(self, validated_data):
unparseable_info = {}
for field, value in self.initial_data.items():
try:
validated_data[field] = self.fields[field].run_validation(value)
except serializers.ValidationError as e:
unparseable_info[field] = value
instance = MetaData.objects.create(**validated_data)
if unparseable_info:
instance.unparseable_info = unparseable_info
instance.save()
return instance
def validate_shipping_address_zip_code(self, value):
if value >= 5:
return value
else:
raise serializers.ValidationError("Message Here")
there's much more validators in serializer look into more detail
https://www.django-rest-framework.org/api-guide/serializers/
I am trying to post to my API with foreign key relationships. It's throwing me back an error saying it's expecting a dictionary as opposed to int for character, character_opponent and stage. This is because the way my models are set up. They have foreign key relationships. The model in question looks like this:
import uuid
from django.db import models
from django.utils import timezone
from analysis.models import Analysis
from characters.models import Character
from stages.models import Stage
class Match(models.Model):
analysis = models.ForeignKey(Analysis, on_delete=models.CASCADE)
character = models.ForeignKey(Character, on_delete=models.CASCADE, related_name='character')
character_won = models.BooleanField()
character_opponent = models.ForeignKey(Character, on_delete=models.CASCADE, related_name='character_opponent')
character_opponent_won = models.BooleanField()
created_at = models.DateTimeField(editable=False)
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
updated_at = models.DateTimeField(editable=False)
stage = models.ForeignKey(Stage, on_delete=models.CASCADE)
def __str__(self):
return '%s vs. %s on %s' % (self.character, self.character_opponent, self.stage)
def save(self, *args, **kwargs):
''' On save, update timestamps '''
if not self.created_at:
self.created_at = timezone.now()
self.updated_at = timezone.now()
return super(Match, self).save(*args, **kwargs)
class Meta:
db_table = "matches"
And here is my serializer:
from rest_framework import serializers
from matches.models import Match
from characters.serializers import CharacterSerializer
from stages.serializers import StageSerializer
class MatchSerializer(serializers.ModelSerializer):
character = CharacterSerializer()
character_opponent = CharacterSerializer()
stage = StageSerializer()
class Meta:
model = Match
fields = ('id', 'analysis', 'character', 'character_won', 'character_opponent', 'character_opponent_won', 'stage')
Is there some option I am missing here to be able to post properly? Clearly I shouldn't have to pass the entire character object each time I want to post something, right? I should just be able to pass the primary key.
From your few comments I understood that you need nested serializer in GET method. What I suggest is, use two[or more] serializers for your API class.
Assuming you are using ModelViewSet API class is using,then you could override get_serializer_class() method as below,
from rest_framework.viewsets import ModelViewSet
class MatchAPI(ModelViewSet):
queryset = Match.objects.all()
def get_serializer_class(self):
if self.action == 'create':
return MatchCreateSerializer
return MatchSerializer
And your MatchCreateSerializer will be like this,
class MatchCreateSerializer(serializers.ModelSerializer):
class Meta:
fields = '__all__'
model = Match
Thus, you only need to provide the PKs of analysis,character etc while creation of Match instance
It will come down to your CharacterSerializer and StageSerializer. If you want to input 1 format (using serialisers.PrimaryKeyRelatedField()), but output another (CharacterSerializer, StageSerializer), you might be best served using 2 serialisers and switching in your view.
In your view you can override get_serializer_class and check your request method, or in the case of a viewset you can check the method being invoked.
When you declare a serializer related field using another serializer, like this
character = CharacterSerializer()
you are telling django-rest-framework that you want a nested serializer. What you want is something like this instead
character = serializers.PrimaryKeyRelatedField()
or you can actually just leave the explicit field declaration out of the serializer (since this is the default), see the doc on serializer relations.
I want to override the built-in django .save() method to perform a check against all other objects in the database.
For example:
class User(models.Model):
name = models.CharField(max_length=120)
class Admin(models.Model):
name = models.CharField(max_length=120)
class SecurityGroup(models.Model):
name = models.CharField(max_length=120)
users = models.ManytoManyField(User)
admins = models.ManytoManyField(Admin)
def save(self, *args, **kwargs):
# check admins don't exist in any other SecurityGroup prior to save
super(SecurityGroup, self).save(*args, **kwargs) # Call the "real" save() method.
The documentation example is pretty simple, and doesn't describe this type of pre-save check.
I have tried adding in lines to .save() such as:
`self.objects.filter(admins__name=self.admins.name).count()`
to call the other SecurityGroup objects but I receive the error:
`Manager is not accessible via SecurityGroup instance`
Is it possible to achieve this save functionality internal to the SecurityGroup Model, or do I need to create a form and use SecurityGroup.save(commit=False) for this type of pre-save check?
Thanks for the help.
The solution that worked for me was to override the Model's form in admin.py. This enabled a simple check whether admins already existed in a SecurityGroup or not.
from django.contrib import admin
from django.forms import ModelForm
from security.models import SecurityGroup
class SecurityGroupAdminForm(ModelForm):
class Meta:
model = SecurityGroup
fields = '__all__'
def clean(self):
# CHECK 1
if admins:
admins = self.cleaned_data['admins']
for a in admins:
existing_group = SecurityGroup.objects.filter(users__username=a.username)
if existing_group:
raise Exception("message")
return self.cleaned_data
Then, within the same admin.py file, indicate the custom form as part of the admin registration for the model of interest (in this case, SecurityGroup):
class UserSecurityGroupAdmin(admin.ModelAdmin):
# class Meta:
model = UserSecurityGroup
form = UserSecurityGroupAdminForm
admin.site.register(UserSecurityGroup, UserSecurityGroupAdmin)
The error is caused by accessing the Manager of a model through a model instance. You should have used
self.model_class().objects
I have a model Owner. The Owner has a type which can be either Individual or Business. Based on type, the Owner may have One-to-One or One-to-Many relationship with Property model. If Individual, it can only have one Property but if it's Business, it may have many Propertys. How do you model this in Django? How can I enforce the Individual to have only one Property.
If you're using PostgreSQL or SQLite you can enforce this with a partial unique index. As of Django 2.2 you can do this declaratively, something like:
from django.db.models import Model, Q, UniqueConstraint
class Property(Model):
...
class Meta:
constraints = [UniqueConstraint(fields=["owner"], condition=Q(type="Individual"))]
Before Django 2.2 you can do this with a migration. Something like:
class Migration(migrations.Migration):
dependencies = [ ... ]
operations = [
migrations.RunSQL("CREATE UNIQUE INDEX property_owner
ON property(owner_id)
WHERE type = 'Individual'"),
]
I would recommend creating the Property model with a ForeignKey to Owner and then add some custom validation by overriding the save function of the class as such.
from django.db import IntegrityError
class Property(models.Model):
owner = models.ForeignKey(Owner)
def save(self, *args, **kwargs):
if self.owner.type == "Individual":
if Property.objects.filter(owner=self.owner).exists():
raise IntegrityError
super(Property, self).save(*args, **kwargs)
This won't cover all cases such as manually adding records to the database or using certain django methods that bypass the save functionality but it will cover a majority of cases.
I have done a pre_save signal in my django/satchmo inherited model Product called JPiece and I have another model inheritance from satchmo Category called JewelCategory. The pre_save signal makes the JPiece objects get the category list and add those categories that fit the Jpiece description to the relation, that is done in the model, meaning if I manually do
p = Jpiece.objects.get(pk=3)
p.save()
The categories are saved and added to the p.category m2m relation but If i save from the admin it does not do this...
How can I achieve this... to save from the admin a JPiece and to get the categories it belongs too...
Here are the models remember that they both have model inheritance from satchmo product and category classes.
class Pieza(Product):
codacod = models.CharField(_("CODACOD"), max_length=20,
help_text=_("Unique code of the piece. J prefix indicates silver piece, otherwise gold"))
tipocod = models.ForeignKey(Tipo_Pieza, verbose_name=_("Piece Type"),
help_text=_("TIPOCOD"))
tipoenga = models.ForeignKey(Engaste, verbose_name=_("Setting"),
help_text=_("TIPOENGA"))
tipojoya = models.ForeignKey(Estilos, verbose_name=_("Styles"),
help_text=_("TIPOJOYA"))
modelo = models.CharField(_("Model"),max_length=8,
help_text=_("Model No. of casting piece."),
blank=True, null=True)
def autofill(self):
#self.site = Site.objects.get(pk=1)
self.precio = self.unit_price
self.peso_de_piedra = self.stone_weigth
self.cantidades_de_piedra = self.stones_amount
self.for_eda = self.for_eda_pieza
if not self.id:
self.date_added = datetime.date.today()
self.name = str(self.codacod)
self.slug = slugify(self.codacod, instance=self)
cats = []
self.category.clear()
for c in JewelCategory.objects.all():
if not c.parent:
if self.tipocod in c.tipocod_pieza.all():
cats.append(c)
else:
if self.tipocod in c.tipocod_pieza.all() and self.tipojoya in c.estilo.all():
cats.append(c)
self.category.add(*cats)
def pieza_pre_save(sender, **kwargs):
instance = kwargs['instance']
instance.autofill()
# import ipdb;ipdb.set_trace()
pre_save.connect(pieza_pre_save, sender=Pieza)
I know I can be vague with explanations sometimes of what I need so please feel free to ask anything Ill be sure to clarify ASAP since this is a client that needs this urgently.
Thank you all as always...
If you use pre_save, it's called before save(), meaning you can't define m2m relationships since the model doesn't have an ID.
Use post_save.
# this works because the ID does exist
p = Jpiece.objects.get(pk=3)
p.save()
Update, check out the comment here: Django - How to save m2m data via post_save signal?
It looks like the culprit now is that with an admin form, there is a save_m2m() happening AFTER the post_save signal, which could be overwriting your data. Can you exclude the field from the form in your ModelAdmin?
# django.forms.models.py
if commit:
# If we are committing, save the instance and the m2m data immediately.
instance.save()
save_m2m()