I’m having trouble creating the Grade resource. Basically, I am trying to make one API call to create an "Estimate" instance that has 3 foreign keys, and would rather use ModelViewSet to use all the methods that come with it (POST, GET, PUT, DELETE, PATCH).
example:
Make one POST call with the following data: {user :, property :, valu_type :, message: "test message"} / API / ratings /
I managed to configure the API to get all the details I need, with the exception of the details of the User object, instead I only get the URL, but I think that’s fine, since I can get the user data on a separate call.
I am a little confused and wanted to understand what is the right approach to create an instance of Evaluation with a single call. Should I override the save method of the Grade class?
All suggestions are welcome.
Django == 1.7.6 djangorestframework == 3.1.0
These are my models:
class Property(models.Model):
user = models.ForeignKey(User)
address = models.CharField(max_length=100)
def __str__(self):
return self.address
class EstimateType(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Estimate(models.Model):
user = models.ForeignKey(User)
property = models.ForeignKey(Property)
estimate_type = models.ForeignKey(EstimateType)
message = models.TextField(max_length=144)
def __str__(self):
return "{} {}".format(self.user.username, self.property.address1)
These are serializers:
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('pk','username', 'email','first_name', 'last_name', 'is_staff')
class PropertySerializer(serializers.ModelSerializer):
user = UserSerializer()
class Meta:
model = Property
fields = ('pk','address','user')
class EstimateTypeSerializer(serializers.ModelSerializer):
class Meta:
model = EstimateType
fields = ('pk', 'name')
class EstimateSerializer(serializers.HyperlinkedModelSerializer):
property = PropertySerializer()
service = EstimateTypeSerializer()
class Meta:
model = Estimate
fields = ('pk','user','property','service', 'message')
ViewSets:
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
class PropertyViewSet(viewsets.ModelViewSet):
queryset = Property.objects.all()
serializer_class = PropertySerializer
class EstimateTypeViewSet(viewsets.ModelViewSet):
queryset = EstimateType.objects.all()
serializer_class = EstimateTypeSerializer
class EstimateViewSet(viewsets.ModelViewSet):
serializer_class = EstimateSerializer
def get_queryset(self):
queryset = Estimate.objects.filter(user=self.request.user)
return queryset
GET request submission / API / evaluation /
Retrieves the following:
[
{
"pk": 3,
"user": "http://localhost:8000/users/2/",
"property": {
"pk": 3,
"address": "123 Fake street",
"user": {
"pk": 2,
"username": "admin",
"email": "admin@foo.com",
"first_name": "Foo",
"last_name": "Bar",
"is_staff": true
}
},
"estimate_type": {
"pk": 1,
"name": "basic"
},
"message": "Lorem Ipsum is simply dummy text of the printing and typesetting \n"
}
]