Skip to content
New issue

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

Allow child one-to-many instances to be updated without providing PK #134

Closed
tsonobe1 opened this issue Sep 3, 2020 · 1 comment
Closed
Assignees
Labels
bug Something isn't working enhancement New feature or request

Comments

@tsonobe1
Copy link
Owner

tsonobe1 commented Sep 3, 2020

The following problems occurred when posting nested json data.
(Using `drf-writable-nested')

  • If the parent's pk is not included in the child's json data, an error ("This field is required.") occurs.

code is bellow

Worries

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']

endpoint for post

payload

{
    "worry": "postman test",
    "goal": "545b498e-93fd-4956-85c7-5bd292704dcd",
    "ideas": [
        {
            "idea": "post man test"
        }
    ]
}

result

"worry": [
        "この項目は必須です。"
    ]

Hurdles

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']

endpoint for post

payload

{
    "hurdle": "post man test",
    "task": 5,
    "solutions": [
        {
            "solution": "post man test"
        }
    ]
}

result

"hurdle": [
        "この項目は必須です。"
    ]
@tsonobe1
Copy link
Owner Author

tsonobe1 commented Sep 3, 2020

Solution

beda-software/drf-writable-nested#29
beda-software/drf-writable-nested#29 (comment)

  • add the required=False

Imported

Worry

class IdeaSerializer(serializers.ModelSerializer):
    class Meta:
        model = Idea
        fields = ['idea_id', 'idea', 'created_at', 'worry']
        extra_kwargs = {
            # This was key!
            "worry":{"required": False}
        }

Hurdle

class SolutionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Solution
        fields = ['solution_id', 'solution', 'created_at', 'hurdle']
        extra_kwargs = {
            # This was key!
            "hurdle":{"required": False}
        }

Post

Worry

post

{
    "worry": "postman test",
    "goal": "545b498e-93fd-4956-85c7-5bd292704dcd",
    "ideas": [
        {
            "idea": "post man test"
        }
    ]
}

result

{
    "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
        }
    ]
}

hurdle

post

{
    "hurdle": "post man test",
    "task": 5,
    "solutions": [
        {
            "solution": "post man test"
        }
    ]
}

###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!

@tsonobe1 tsonobe1 closed this as completed Sep 3, 2020
@tsonobe1 tsonobe1 self-assigned this Sep 3, 2020
@tsonobe1 tsonobe1 added enhancement New feature or request bug Something isn't working labels Sep 3, 2020
@tsonobe1 tsonobe1 mentioned this issue Sep 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant