this is a simple question but I'm very new to django-rest-framework.
I was wondering if there is any way to access a method defined on the model from the serializer.?
Say I have a model
class Listing(models.Model):
listingid = models.BigIntegerField(primary_key=True)
mappingid = models.BigIntegerField()
projectlevelid = models.IntegerField()
subsellerid = models.IntegerField()
iscreatedbyadmin = models.BooleanField(default=None, null=True)
createdon = models.DateTimeField(auto_now_add=True, editable=False)
validationstatus = models.SmallIntegerField(default=0)
def is_project(self):
""" Returns True if listing is of Project Type (projectlevelid=6) else False"""
if self.projectlevelid == 6:
return True
else:
return False
def get_project_info(self):
"""Returns False if listing is not mapped to a project, else returns the project info"""
if self.is_project() == False:
return False
return models.Project.objects.get(projectid=self.mappingid)
Is it possible for the serializer
class ListingSerializer(serializers.ModelSerializer):
class Meta:
model = models.MasterListing
to have access to Listing.is_project i.e. for an object of the Listing model, can the serializer call its is_project method?
If so, can I set a field in the serializer such that if is_project returns true, the field is populated?
I am trying for something like this,
class ListingSerializer(serializers.ModelSerializer):
project = serializers.SomeRELATEDFieldTYPE() # this field if populated if the `is_project` is true
class Meta:
model = models.MasterListing
I understand I can do this using some combination of required=False and SerializerMethodField, but maybe there is a simpler way?.
Note: It is not possible for me to set a foreign key to the mappingid, since it depends on the projectlevelid. I also can't affect this relationship so no further normalization is possible. I know that there might be some way using content-types, but we are trying to avoid that if it is possible..
EDIT: I solved the problem, but not as the question specified.
I used this:
class ListingSerializer(serializers.ModelSerializer):
project = serializers.SerializerMethodField()
def get_project(self, obj):
"""Returns False if listing is not mapped to a project, else returns the project info"""
if str(obj.projectlevelid) == str(6):
projectObj = models.Project(projectid=obj.mappingid)
projectObjSerialized = ProjectSerializer(projectObj)
return projectObjSerialized.data
return False
class Meta:
model = models.MasterListing
So, the original question still stands: "Is it possible for the modelSerializer to access its models methods?"
Also, another problem that now appears is, can I make the serializer exclude fields on demand, i.e. can it exclude mappingid and projectlevelid if it is indeed a project?
For your first question source attribute is the answer, citing:
May be a method that only takes a self argument, such as
URLField('get_absolute_url')
For your second answer, yes it is also possible. Check the example it provides in their docs: http://www.django-rest-framework.org/api-guide/serializers/#dynamically-modifying-fields
PS: I really love drf for its very complete documentation =).
EDIT
To use the source attribute you can just declare a new explicit field like so:
is_project = serializers.BooleanField(source='is_project')
With this, is_project field has the value of the is_project method of your instance. Having this, when creating the dynamic serializer (by modifying its init method) you can add the 'project' field if it's True.
#argaen is absolutely right, source is a DRF core argument, and would most definitely solve your problem. However, it's redundant to use source, if the field name is the same as the source. So the above answer won't require you specify source, since field name is_project is the same as source name is_project.
So in your case:
is_project = serializers.BooleanField()
Related
I am creating a Django model where:
expirationTimeStamp field's default value is based on creationTimeStamp
isLive boolean field value is based on expirationTimeStamp
I have written the following functions expirationTimeCalculation and postLiveStatus and assigned them as default values to the fields but I am getting error. I have also tried to assign to respective fields through property(function) yet I am still getting error.
One of the functionality that I need to implement is that user can send custom expirationTimeStamp as well that would override default value, therefore, I believe property(function) assignment is not ideal for expirationTimeStamp field.
Is there any other way that I can go about to set the expirationTimeStamp field value based on creationTimeStamp field?
Any feedback is appreciated!
class Posts(models.Model):
def expirationTimeCalculation(self):
EXPIRATION_DURATION = 86400 #time in seconds
expirationTime = self.creationTimestamp + timedelta(seconds = EXPIRATION_DURATION)
return expirationTime
def postLiveStatus(self):
return (self.expirationTimestamp > timezone.now)
message = models.TextField()
creationTimestamp = models.DateTimeField(default=timezone.now)
expirationTimestamp = models.DateTimeField(default=expirationTimeCalculation)
isLive = models.BooleanField(default=postLiveStatus)
Similar answered question. I am attaching the official documentation as well as the link to the answered question.
Models certainly do have a "self"! It's just that you're trying to define an attribute of a model class as being dependent upon a model instance; that's not possible, as the instance does not (and cannot) exist before your define the class and its attributes.
To get the effect you want, override the save() method of the model class. Make any changes you want to the instance necessary, then call the superclass's method to do the actual saving. Here's a quick example.
def save(self, *args, **kwargs):
if not self.subject_init:
self.subject_init = self.subject_initials()
super(Subject, self).save(*args, **kwargs)
Model Overriding Documentation
Django Model Field Default Based Off Another Field
I needed field end_day to default to the last day of month in serializer. So I did this:
class MySerializer(serializers.Serializer):
year = serializers.IntegerField()
month = serializers.IntegerField()
end_day = serializers.IntegerField(required=False, default=None)
def end_day_value(self):
return self.validated_data['end_day'] or \
(datetime(year=self.validated_data['year'], month=self.validated_data['month'])+relativedelta(months=1)-relativedelta(days=1)).day
So when I need end_day or its default value I just call end_day_value method.
I am currently trying to familiarize myself with DRF and while going through a tutorial these serializers were used
class EmbeddedAnswerSerializer(serializers.ModelSerializer):
votes = serializers.IntegerField(read_only=True)
class Meta:
model = Answer
fields = ('id', 'text', 'votes',)
class QuestionSerializer(serializers.ModelSerializer):
answers = EmbeddedAnswerSerializer(many=True,source='answer_set')
class Meta:
model = Question
fields = ('id', 'answers', 'created_at', 'text', 'user_id',)
These are the models
class Question(models.Model):
user_id = models.CharField(max_length=36)
text = models.CharField(max_length=140)
created_at = models.DateTimeField(auto_now_add=True)
class Answer(models.Model):
question = models.ForeignKey(Question,on_delete=models.PROTECT)
text = models.CharField(max_length=25)
votes = models.IntegerField(default=0)
My question is in the statement in the Question serializer
answers = EmbeddedAnswerSerializer(many=True,source='answer_set')
what is the purpose of many = True and source='answer_set' ?
I read from the documentation the following regarding many=True
You can also still use the many=True argument to serializer classes.
It's worth noting that many=True argument transparently creates a
ListSerializer instance, allowing the validation logic for list and
non-list data to be cleanly separated in the REST framework codebase.
I am confused by what that means? If I remove many=True from the code I get the error
AttributeError at /api/quest/1/2/
Got AttributeError when attempting to get a value for field `text` on serializer `EmbeddedAnswerSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `RelatedManager` instance.
Original exception text was: 'RelatedManager' object has no attribute 'text'.
Can anyone explain what many=True does and what source field does?
Adding to the answer above by #neverwalkaloner
Many = True
many=True signals that there is more than one object (an iterable) being passed to the serializer. Passing this field in turn triggers the many_init within BaseSerializer to automagically create a ListSerializer instance.
Source Code Snippet:
def __new__(cls, *args, **kwargs):
# We override this method in order to automagically create
# `ListSerializer` classes instead when `many=True` is set.
if kwargs.pop('many', False):
return cls.many_init(*args, **kwargs)
return super(BaseSerializer, cls).__new__(cls, *args, **kwargs)
Source = "xyz"
This tells DRF which object attribute supplies the value for the field. The default assumption is that the field name declared on the serializer is the same as the field on the object instance that supplies the value. In cases where this is not true, source allows you to explicitly supply the object instance where the serializer will look for the value. Here's a peek into the def bind(self, field_name, parent) inside serializers.fields where this happens
Source Code Snippet:
# self.source should default to being the same as the field name.
if self.source is None:
self.source = field_name
# self.source_attrs is a list of attributes that need to be looked up
# when serializing the instance, or populating the validated data.
if self.source == '*':
self.source_attrs = []
else:
self.source_attrs = self.source.split('.')
Finally the value is gotten as follows using the source and source_attrs declared in bind:
def get_attribute(self, instance):
"""
Given the *outgoing* object instance, return the primitive value
that should be used for this field.
"""
try:
return get_attribute(instance, self.source_attrs)
except (KeyError, AttributeError) as exc:
Assuming a Question can have multiple Answers, your approach is correct.
The problem appears to be that the source you supplied is the RelatedManager instance itself, and not the queryset of Answer objects. I assumed DRF resolves this accurately, can you try changing it to source='answer_set.all'.
answer_set is the default RelatedManager name given by Django. It might be wise to name your backward relationship using related_name in the Django model. This can be achieved by changing:
question = models.ForeignKey(Question,on_delete=models.PROTECT, related_name='answers')
Probably not the best explanation and someone can add more details but briefly many=True tells to serializer that it will take list of objects for serilizing proccess. In other words it's just a trigger that allows you to specify will you serialize, well, many objects at once, or just single object.
source on the other side specify which attribute of objects should be serializing with current serializer's field.
In practice this line
answers = EmbeddedAnswerSerializer(many=True, source='answer_set')
means that you want to serialize answer_set attribute of Question object with EmbeddedAnswerSerializer. Since answer_set is list of object you should add many=True as argument to make serializer aware that it will work with list of objects instead of single object.
I'm trying to make a CreateView have a readonly field with a set value, but I'm unable to make that work.
I have a model with a ForeignKey to another model:
class CompanyNote(TimeStampedModel):
company = models.ForeignKey(Company)
note = models.TextField(blank=True)
And I have a CreateView:
class CompanyNoteCreateView(CreateView):
model = models.CompanyNote
form_class = CompanyNoteForm
That uses a custom ModelForm:
class CompanyNoteForm(forms.ModelForm):
company = forms.ChoiceField(
widget=forms.widgets.Select(attrs={'readonly': 'readonly'}))
class Meta:
model = models.CompanyNote
As you see, the widget for the field in question is readonly. This is because I pick up the company as a part of the URL, as in company/1/note/add . I have no trouble picking up the "1" and finding the right company object, but I don't know how to set the readonly field.
I tried:
def get_initial(self):
initial = super(CompanyNoteCreateView, self).get_initial()
initial['company'] = self.get_company().id
return initial
But that didn't work. The Widget is empty, which may be the problem. Perhaps I'm barking up the wrong tree here. Any ideas welcome.
Have you tried setting the attribute in the Form's Meta class?
I experienced an issue where Form attributes were not applied for Model Fields if set in the base class definition, but they worked correctly in the Meta class:
class CompanyNoteForm(forms.ModelForm):
class Meta:
model = models.CompanyNote
widgets = {'company': forms.widgets.Select(attrs={'readonly': True,
'disabled': True})}
Otherwise check this answer out.
Worst case scenario, make company a hidden field?
Use a ModelChoiceField
class CompanyNoteForm(forms.ModelForm):
company = forms.ModelChoiceField(queryset=models.Company.objects.all(), widget=forms.widgets.Select(attrs={'readonly': 'readonly'}))
I could not find this answer anywhere, that I could actually get to work. But I found a different approach. Set the field to be hidden with forms.HiddenInput() widget. Then the value you pass in from the view will be assigned but the user cannot access it.
widgets = {'field_name': forms.HiddenInput()}
I'm using ModelForm class so my syntax might be different from yours.
My question is: is there a way to create custom model form that will use a specified field from a related model rather than the related model's id?
To clarify, if I have the following two models:
class ModelOne(models.Model):
id = models.AutoField(primary_key = True)
name = models.CharField(unique = True, blank = False, null = False)
class ModelTwo(models.Model):
id = models.AutoField(primary_key = True)
parent = models.ForeignKey(ModelOne, blank = False, null = False)
attribute_1 = models.CharField(blank = False, null = False)
attribute_2 = models.IntegerField(blank = False, null = False)
Now if I create an UpdateView on ModelTwo using a ModelForm then the parent field will be pre-filled with the corresponding id from ModelOne. However I want it to display the name attribute of ModelOne and then on form submission parse the unique name (of ModelOne) to the corresponding instance of ModelOne. The reason I want to do it this way, is that I believe it is far more intuitive from a users perspective to deal with the name of ModelOne (when updating a ModelTwo instance) rather than its "id".
Any suggestions of how I can do this?
Firstly, try defining the unicode method on ModelOne. It might not apply to the solution, but it's worth having - it will drive the text values in a form Select widget...
def __unicode__(self):
'''Warning: be careful not to use related objects here,
could cause unwanted DB hits when debugging/logging
'''
return self.name
If that's not sufficient, something like this might work (it is adapted from a form I have that updates the user's name attached to a profile)...
class M2Form(forms.ModelForm):
m1_name = forms.CharField()
class Meta:
model = ModelTwo
def save(self, *args, **kw):
# Update your name field here, something like
if self.cleaned_data.get('m1_name'):
self.instance.parent = ModelOne.objects.get(name=self.cleaned_data.get('m1_name'))
return super(M2Form, self).save(*args, **kw)
This is untested, and you'll likely need to adapt this to validate that the name exists and make sure the original parent field doesn't appear on the form. With any luck, the first answer covers what I think your question is.
Using Rog's answer as a starting point and delving through some of Django's internals I eventually came to a working solution. Given my level of Django knowledge, I imagine there is a better way of doing this; so if you have another method please add it.
So based on the above two models, I created the following form class:
class CustomForm(forms.ModelForm):
parent = models.CharField(label='Name')
class Meta:
model = ModelTwo
exclude = ['parent']
def __init__(self,*args,**kwargs):
# The line of code below is the one that I was looking for. It pre-populates
# the "parent" field of the form with the "name" attribute of the related
# ModelOne instance.
kwargs['initial']['parent'] = kwargs['instance'].parent.name
super(CustomForm,self).__init__(*args,**kwargs)
# The next line is for convenience and orders the form fields in our desired
# order. I got this tip from:
# http://stackoverflow.com/questions/913589/django-forms-inheritance-and-order-of-form-fields
self.fields.keyOrder = ['parent','attribute_1','attribute_2']
def save(self, *args, **kwargs):
if self.cleaned_data.get('parent'):
# This section of code is important because we need to convert back from the
# unique 'name' attribute of ModelOne to the corresponding instance so that
# ModelTwo can be saved. Thanks goes to Rog for this section of code.
self.instance.parent = ModelOne.objects.get(name=self.cleaned_data.get('parent'))
return super(CustomForm, self).save(*args, **kwargs)
I have a model with a field that is required but not entered by the user and i have a hard time saving the model without errors. My model definition looks like this:
class Goal(db.Model):
author = db.UserProperty(required=True)
description = db.StringProperty(multiline=True, required=True)
active = db.BooleanProperty(default=True)
date = db.DateTimeProperty(auto_now_add=True)
class GoalForm(djangoforms.ModelForm):
class Meta:
model = Goal
exclude = ['author', 'active']
And i use django-forms in appengine to create and validate the form. When i try to save the result of this form however....
def post(self):
data = GoalForm(data=self.request.POST)
if data.is_valid():
goal = data.save(commit=False)
goal.author = users.get_current_user()
goal.put()
self.redirect('/')
I get "ValueError: The Goal could not be created (Property author is required)"
Now i would think that by having commit=False, then adding the property for Goal, and then saving the object would allow me to do this but obviously it's not working. Any ideas?
Note that save() will raise a ValueError if the data in the form doesn't validate
You can find what you need about the save() method here:
http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-method
Edit: Instead of goal.put(), do a goal.save()
Edit2: This should solve your problem:
goal = Goal(author='Mr') #example
data = GoalForm(data=self.request.POST, instance=goal)
I realize this is an old question, but for the sake of others searching for a similar answer, I'm posting the following:
Unless there's a reason I missed for not doing so, but I believe this is what you need:
class Goal(db.Model):
author = db.UserProperty(auto_current_user_add=True)
...
...
For reference:
Types and Property Classes:class UserProperty()
Your GoalForm should inherit from django.forms.ModelForm and be defined such that it only requires some fields:
class GoalForm(django.forms.ModelForm):
class Meta:
model = Goal
fields = ('description', 'etc')
Not sure if this is totally working in AppEngine though.
You should also save the form (still not sure about AppEngine):
data = GoalForm(data=self.request.POST)
if data.is_valid():
data.author = users.get_current_user()
data.put()