1

Assuming I have this models.py:

class HRSubject(modes.Model):
    group = models.ForeignKey(Group, related_name="hrsubjects")
    name = models.CharField(max_length=256)

and my serializes are:

class HRSubjectSerializer(ModelSerializer):
    group = GroupSerializer(many=False)

    class Meta:
         model = HRSubject

ok, now i have a model view set for the HRSubject model. I want to be able to perform an update on the group property, replace this group with other one.

Before Update:

{
"id": 1, 
"group": {
    "id": 1, 
    "name": "SomeGroup"
}, 
"name": "Info"
}

After Update:

{
"id": 1, 
"group": {
    "id": 2, 
    "name": "SecondGroup"
}, 
"name": "Info"
}

How can i accomplish this with the wonderful django-rest-framework 3 package?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tal Peretz
  • 515
  • 1
  • 5
  • 15
  • Almost [similar](http://stackoverflow.com/a/27585066/4228942) – Sam R. Dec 27 '14 at 20:35
  • Not exactly, I need to be able to replace the group instance with other group from my db. This example doesn't show this option. – Tal Peretz Dec 27 '14 at 20:42
  • Post your `Group` model as well. Cuz it's really confusing what you're doing here. Also, `replace` means `update`, not? – Sam R. Dec 27 '14 at 20:54
  • What is the first `id` and what is the 2nd `id`? – Sam R. Dec 27 '14 at 20:55
  • My group model is the regular Group model that includes with django. I am trying to update the group instance in my HrSubject. I need to able to replace a group by other group. Now my instance has group with I'd equal to 1 and I want to replace it with a group with Id 2. – Tal Peretz Dec 27 '14 at 21:07
  • Perhaps I'm getting this wrong, I believe you can just update `group_id` to **2** since `id` is a foreign key, and you don't need to update `group.id`? – Anzel Dec 27 '14 at 21:47
  • If I'm using a flat representation your right. But when I'm using the group serliazer I can't! I'm able to update the current group, can't change the Id property. – Tal Peretz Dec 28 '14 at 08:05

0 Answers0