I'm relatively new to DJango and I am looking to add data to a many-to-many field using serializers from rest framework.
我对DJango比较陌生,我希望使用rest framework中的序列化器将数据添加到多对多字段中。
My Model:
我的型号:
class IngFamily(models.Model):
name=models.CharField(max_length=30, unique=True, verbose_name='ingredient parent')
class UserProfile(models.Model):
user=models.ForeignKey(User)
allergies=models.ManyToManyField(IngFamily, verbose_name='allergies', null=True)
Now, I want to add data to the UserProfile model using the rest api.
现在,我想使用其余的api将数据添加到UserProfile模型。
I've looked around, but couldn't find much anywhere.
我环顾四周,但在任何地方都找不到。
So far what I've achieved:
到目前为止我取得的成就:
serializer:
串行:
class IngFlySerializer(serializers.ModelSerializer):
class Meta:
model = IngFamily
fields= ('name',)
class UserProfileSerializer(serializers.ModelSerializer):
allergies=IngFlySerializer(many=True,)
class Meta:
model = UserProfile
fields=('allergies',)
def create(self, validated_data):
allergies_data =validated_data.pop('allergies', [])
#import pdb;pdb.set_trace()
user1=UserProfile.objects.create(**validated_data)
for allergy in allergies_data:
al=IngFamily.objects.filter(name=allergy.get('name'))
#al.name=allergy.get('name')
user1.allergies.add(al)
user1.save()
return user1
When I try using this, "al" is empty.
当我尝试使用它时,“al”是空的。
view:
视图:
class user_profile(generics.ListCreateAPIView):
serializer_class = UserProfileSerializer
permission_classes = (permissions.IsAuthenticated,)
def get_queryset(self):
user = self.request.user
return UserProfile.objects.filter(user=user)
def perform_create(self, serializer):
#import pdb; pdb.set_trace()
serializer.save(user=self.request.user)
class UserDetail(generics.RetrieveDestroyAPIView):
serializer_class = UserProfileSerializer
permission_classes = (permissions.IsAuthenticated,)
def get_queryset(self):
user1 = self.request.user
return UserProfile.objects.filter(user=user1)
My Post request would look this:
我的帖子请求看起来像这样:
{"allergies": [{"name": ["Beef"]},{"name": ["Pork"]}]}
I've been stuck on this for a while, any help would be appreciated :)
我已经坚持了一段时间,任何帮助将不胜感激:)
1 个解决方案
#1
0
Couple of things can be tried :
可以尝试几件事:
- It might be possible your database dont have some of the allergies coming in with user data. Thats why your al is coming empty.
- 您的数据库可能可能没有用户数据中的某些过敏症。这就是为什么你的al会变空。
- saving user data before you add allergies to it. This is important.
- 在添加过敏症之前保存用户数据。这个很重要。
- By using ipdb or pdb, try to understand in what data form 'allergy' and 'allergies_data' is coming out. If indeed it is what you have written, there is no reason the following code shouldnt work.
- 通过使用ipdb或pdb,尝试了解'过敏'和'allergies_data'的数据形式。如果确实是你所写的,那么以下代码就没有理由不起作用。
def create(self, validated_data):
allergies_data =validated_data.pop('allergies')
user1=UserProfile.objects.create(**validated_data)
user1.save()
for allergy in allergies_data:
al=IngFamily.objects.get_or_create(name=allergy['name'])
user1.allergies.add(al)
return user1
#1
0
Couple of things can be tried :
可以尝试几件事:
- It might be possible your database dont have some of the allergies coming in with user data. Thats why your al is coming empty.
- 您的数据库可能可能没有用户数据中的某些过敏症。这就是为什么你的al会变空。
- saving user data before you add allergies to it. This is important.
- 在添加过敏症之前保存用户数据。这个很重要。
- By using ipdb or pdb, try to understand in what data form 'allergy' and 'allergies_data' is coming out. If indeed it is what you have written, there is no reason the following code shouldnt work.
- 通过使用ipdb或pdb,尝试了解'过敏'和'allergies_data'的数据形式。如果确实是你所写的,那么以下代码就没有理由不起作用。
def create(self, validated_data):
allergies_data =validated_data.pop('allergies')
user1=UserProfile.objects.create(**validated_data)
user1.save()
for allergy in allergies_data:
al=IngFamily.objects.get_or_create(name=allergy['name'])
user1.allergies.add(al)
return user1