We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The following problems occurred when posting nested json data. (Using `drf-writable-nested')
code is bellow
class Worry(models.Model): goal = models.ForeignKey( Goal, on_delete=models.CASCADE, related_name='worries') worry_id = models.AutoField(primary_key=True) worry = models.TextField(verbose_name='心配事', null=True, blank=True) created_at = models.DateTimeField( verbose_name='作成日時', default=timezone.now) def __str__(self): return self.worry or '' class Idea(models.Model): worry = models.ForeignKey( Worry, on_delete=models.CASCADE, related_name='ideas') idea_id = models.AutoField(primary_key=True) idea = models.TextField(verbose_name='対策', null=True, blank=True) created_at = models.DateTimeField( verbose_name='作成日時', default=timezone.now) def __str__(self): return self.idea or '' class IdeaSerializer(serializers.ModelSerializer): class Meta: model = Idea fields = ['idea_id', 'idea', 'created_at', 'worry'] class WorrySerializer(WritableNestedModelSerializer): ideas = IdeaSerializer(many=True, required=False, allow_null=True) class Meta: model = Worry fields = ['worry_id', 'worry', 'created_at', 'goal', 'ideas']
{ "worry": "postman test", "goal": "545b498e-93fd-4956-85c7-5bd292704dcd", "ideas": [ { "idea": "post man test" } ] }
"worry": [ "この項目は必須です。" ]
class Hurdle(models.Model): task = models.ForeignKey( Task, on_delete=models.CASCADE, related_name='hurdles') hurdle_id = models.AutoField(primary_key=True) hurdle = models.TextField(verbose_name='障害', null=True, blank=True) created_at = models.DateTimeField( verbose_name='作成日時', default=timezone.now) def __str__(self): return self.hurdle or '' class Solution(models.Model): hurdle = models.ForeignKey( Hurdle, on_delete=models.CASCADE, related_name='solutions') solution_id = models.AutoField(primary_key=True) solution = models.TextField(verbose_name='解決策', null=True, blank=True) created_at = models.DateTimeField( verbose_name='作成日時', default=timezone.now) def __str__(self): return self.solution or '' class SolutionSerializer(serializers.ModelSerializer): class Meta: model = Solution fields = ['solution_id', 'solution', 'created_at', 'hurdle'] extra_kwargs = { # This was key! "hurdle":{"required": False} } class HurdleSerializer(WritableNestedModelSerializer): solutions = SolutionSerializer(many=True, required=False, allow_null=True) class Meta: model = Hurdle fields = ['hurdle_id', 'hurdle', 'created_at', 'task', 'solutions']
{ "hurdle": "post man test", "task": 5, "solutions": [ { "solution": "post man test" } ] }
"hurdle": [ "この項目は必須です。" ]
The text was updated successfully, but these errors were encountered:
beda-software/drf-writable-nested#29 beda-software/drf-writable-nested#29 (comment)
required=False
class IdeaSerializer(serializers.ModelSerializer): class Meta: model = Idea fields = ['idea_id', 'idea', 'created_at', 'worry'] extra_kwargs = { # This was key! "worry":{"required": False} }
class SolutionSerializer(serializers.ModelSerializer): class Meta: model = Solution fields = ['solution_id', 'solution', 'created_at', 'hurdle'] extra_kwargs = { # This was key! "hurdle":{"required": False} }
{ "worry_id": 68, "worry": "postman test", "created_at": "2020-09-04T07:53:50.508206+09:00", "goal": "545b498e-93fd-4956-85c7-5bd292704dcd", "ideas": [ { "idea_id": 127, "idea": "post man test", "created_at": "2020-09-04T07:53:50.520737+09:00", "worry": 68 } ] }
###result
{ "hurdle_id": 5, "hurdle": "post man test", "created_at": "2020-09-03T07:40:49.153273+09:00", "task": 5, "solutions": [ { "solution_id": 18, "solution": "post man test", "created_at": "2020-09-03T07:40:49.169833+09:00", "hurdle": 5 } ] }
OK!
Sorry, something went wrong.
tsonobe1
No branches or pull requests
The following problems occurred when posting nested json data.
(Using `drf-writable-nested')
code is bellow
Worries
endpoint for post
payload
result
Hurdles
endpoint for post
payload
result
The text was updated successfully, but these errors were encountered: