Home > Mobile >  Copying only non-relation attributes django/python
Copying only non-relation attributes django/python

Time:11-27

I am copying a model object to another, but I want that it doesn’t copy the relations

For example, assume you have a model like this:

class Dish(models.Model):
    name = models.CharField(max_length=100)
    description = models.CharField(max_length=500)
    category = models.ForeignKey(Category, on_delete=models.CASCADE, default=1)

    def __str__(self):
        return self.name

Then I do:

            my_dish = Dish.objects.get(pk=dish.id)
            serializer = Dish_Serializer(my_dish)
            my_new_object = serializer.data

I want my_new_object to include only those attributes that are not relations, in this case, name and description.

How do I do that without accessing name and description directly?

CodePudding user response:

I assume in your serializer you don't want to explicitly define which field to serialize. Otherwise you could do the following:

class Dish_Serializer(serializers.ModelSerializer):
    class Meta:
        model = Dish
        fields = ['id','name', 'description']

You probably can define these fields dynamically:

        fields = [f.name for f in Dish._meta.concrete_fields]                                                                                         

or

        fields = [f.name for f in Dish._meta.fields if not isinstance(f,ForeignKey)]

CodePudding user response:

Ultimately, you want my_new_object in dictionary format and as per condition pk will give you only one object of dish. So, you can do this instead :

my_new_object = Dish.objects.filter(pk=dish.id).values("name", "description")[0]

It will give you exact what you want, just declare the fields you need in values as an attribute fields.

  • Related