Answer the question
In order to leave comments, you need to log in
How to solve error when updating ManyToManyField data?
So, I have an entry point /surveys//. I access it with the PUT method to update the data and pass the following data:
{
"id": 135,
"statuses": [],
"name": "asdasdasdsad",
"description": "asdasdasdasd",
"unique_url": "eecc2d5de02543bfba909d1660f8798f",
"is_active": false,
"is_anonymous": false,
"have_answers": false,
"created_at": "2018-08-30T20:02:29.231761",
"answers_count": 0,
"expiration_date": null,
"creator": 167
}
Cannot set values on a ManyToManyField which specifies an intermediary model. Use surveys.Status's Manager instead.
class Survey(models.Model):
name = models.CharField(max_length=255, help_text="input survey name", verbose_name='survey name')
description = models.TextField(blank=True,
help_text="input description")
creator = models.ForeignKey(User, models.CASCADE, help_text="creator survey")
unique_url = models.CharField(max_length=200, null=True, unique=True, verbose_name='url')
is_active = models.BooleanField(null=False, default=False)
is_anonymous = models.BooleanField(null=False, default=False)
have_answers = models.BooleanField(default=False)
statuses = models.ManyToManyField(User, through='Status', related_name='statuses')
created_at = models.DateTimeField(auto_now_add=True,null=True)
answers_count = models.IntegerField(default=0)
expiration_date = models.DateField(null=True, default=None, blank=True)
class Meta:
verbose_name = 'Опрос'
def __str__(self):
return "Survey. Name: %s, Description: %s" % (
self.name, self.description)
class Status(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE,related_name='status_user')
survey = models.ForeignKey(Survey, on_delete=models.CASCADE)
is_passed = models.BooleanField(default=False)
class Meta:
unique_together = ('user', 'survey')
def finish_survey(self):
self.is_passed = True
self.save()
class StatusSerializer(serializers.ModelSerializer):
class Meta:
model = Status
fields = ('__all__')
class SurveySerializer(serializers.ModelSerializer):
statuses = StatusSerializer(many=True,required=False, read_only=True)
class Meta:
model = Survey
fields = ('__all__')
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question