4

I am stuck on trying to solve an issue with the Serializers and related fields using the django-rest-framework. Currently I have a model that looks like this:

class DataSetModel(models.Model):
    title = models.CharField(max_length=200)
    description = models.TextField()

class DataPointModel(models.Model):
    dataSet = models.ForeignKey(DataSetModel, related_name='dataPoints')
    label = models.CharField(max_length=200)

My serializers look like this:

class DataPointSerializer(serializers.ModelSerializer):
    class Meta:
        model = DataPointModel
        fields = ('pk','label')

class DataSetSerializer(serializers.ModelSerializer):
    dataPoints = DataPointSerializer(many=True, read_only=True)

    class Meta:
        model = DataSetModel
        fields = ('pk','title')

The problem I am having is when I try to change the "many=False" in the serializer produces this error:

Got AttributeError when attempting to get a value for field label on serializer DataPointSerializer. 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 'label'.

Since this is only ever one model object (one-to-many relationship), I want to get the result as a single object vs a list of one object.

Am I doing this the right way? I thought that turning the "many=False" it would fetch the first record in an nested query.

Any insight would be greatly appreciated.

3 Answers 3

1

You can't set many=False, because dataPoints is a related field that returns a queryset containing a list of instances, not just an instance.

When you do DataPointModel.dataPoints that returns a queryset, it can't returns just an instance. So setting many=False, it wouldn't get the first element of the list.

Sign up to request clarification or add additional context in comments.

3 Comments

Levi, thanks for the answer. How would you suggest handling this in my Model set-up?
@renderbox why is your point? what do you want to do ? render just one instance model ?
I am nesting the serialized model so it's just one call to the server. The point is to not have to go through extra processing on the client end (of processing a list) and having it be delivered ready to be consumed.
1

So the solve I came to was refactoring my models with where the Foreign Keys are attached.

Here is the fix I implemented:

class DataSetModel(models.Model):
    title = models.CharField(max_length=200)
    description = models.TextField()
    dataPoint = models.ForeignKey(DataPointModel)

class DataPointModel(models.Model):
    label = models.CharField(max_length=200)

Moving it to the DataSetModel automatically joins the data model if you set 'many=False' in the Serializer object.

Comments

1

Write your DataPointModel as below. It should work then

class DataPointModel(models.Model):
    dataSet = models.OneToOneField(DataSetModel, related_name='dataPoints')
    label = models.CharField(max_length=200)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.