django-serializer

Django Rest Framework - how to write multiple nested field serializer (for reading & writing)

倾然丶 夕夏残阳落幕 提交于 2021-02-19 08:33:58
问题 I'm trying to write a "def create" method to perform nested serialization for multiple objects. def create(self, validated_data): suggested_songs_data = validated_data.pop('suggested_songs') suggest_song_list = list() for song_data in suggested_songs_data: song = Song.objects.create(**song_data) suggest_song_list.append(song) message = Messages.objects.create(suggested_songs=suggest_song_list, **validated_data) return message Here is my schema: class MessagesSerializer(serializers

Django Rest Framework - how to write multiple nested field serializer (for reading & writing)

二次信任 提交于 2021-02-19 08:33:54
问题 I'm trying to write a "def create" method to perform nested serialization for multiple objects. def create(self, validated_data): suggested_songs_data = validated_data.pop('suggested_songs') suggest_song_list = list() for song_data in suggested_songs_data: song = Song.objects.create(**song_data) suggest_song_list.append(song) message = Messages.objects.create(suggested_songs=suggest_song_list, **validated_data) return message Here is my schema: class MessagesSerializer(serializers

`basename` argument not specified, and could ' \

£可爱£侵袭症+ 提交于 2021-02-19 04:53:10
问题 I am getting the below error again and again.I am trying to solve it from morning but nothing is happening. assert queryset is not None, ' basename argument not specified, and could ' \ AssertionError: basename argument not specified, and could not automatically determine the name from the viewset, as it does not have a .queryset attribute. models.py class Language(models.Model): A = models.CharField(max_length=50) B = models.ForeignKey(User,on_delete=models.CASCADE,null=True) C = models

Create object only if other object is successfully created

回眸只為那壹抹淺笑 提交于 2021-02-18 19:09:57
问题 I am fairly new to Django and unfamiliar with the best practices for this situation (in any framework/language, not just python/django). The situation is that when a user first registers on my site, I want to create an "organization" for them if it doesn't exists, and then subsequently create a user for them, which references the organization. I never want to insert one without the other, but I need to create the organization first so that the organization UUID can be saved for each user.

Create object only if other object is successfully created

痞子三分冷 提交于 2021-02-18 19:04:02
问题 I am fairly new to Django and unfamiliar with the best practices for this situation (in any framework/language, not just python/django). The situation is that when a user first registers on my site, I want to create an "organization" for them if it doesn't exists, and then subsequently create a user for them, which references the organization. I never want to insert one without the other, but I need to create the organization first so that the organization UUID can be saved for each user.

Nested Json is not being serialized while using Multipart Parser

南楼画角 提交于 2021-02-11 15:09:40
问题 I will try to give a small example and tell where it's not working. The models are as follows, class Address(models.Model): name = models.CharField(db_column='name', max_length=200, blank=False, null=False, unique=True) class Meta: managed = True db_table = 'Address' class Product(models.Model): title = models.CharField(db_column='title', max_length=200, blank=False, null=False) imageUrl = models.ImageField(db_column='image_url', blank=True, null=True, upload_to='deals/%Y/%m/') addresses =

How do I create a serializer that reuses a unique key of my model?

牧云@^-^@ 提交于 2021-02-07 10:36:56
问题 I'm using Python 3.7, Django 2.2, the Django rest framework, and pytest. I have the following model, in which I want to re-use an existing model if it exists by its unique key ... class CoopTypeManager(models.Manager): def get_by_natural_key(self, name): return self.get_or_create(name=name)[0] class CoopType(models.Model): name = models.CharField(max_length=200, null=False, unique=True) objects = CoopTypeManager() Then I have created the below serializer to generate this model from REST data

non trivial django serializer for a complex model

假如想象 提交于 2021-01-29 18:40:51
问题 After many years I code with Java, I got an interesting project at work to be developed using python django rest framework. I have a complex model, with many relations, in which I want to create entities in DB based on one JSON request. Please refer to attached screenshot of my DB model. My goal is to create new projects in the DB where I have set of companies in DB and a freelancer can suggest to a project of the company. I would like to get a JSON request that contains: company id (ALREADY

python ForeignKey relation with user shows this error NOT NULL constraint failed: music_song.performer_id

北战南征 提交于 2021-01-29 15:45:15
问题 Fellow programmers, I am getting a error NOT NULL constraint failed: music_song.performer_id I want the performer field to be the user who is creating the song but its not happening I have tried a lot can you guys please help me out with it models.py class Song(models.Model): name = models.CharField(_("Name"), max_length=50) song = models.FileField(_("Song"), upload_to=None, max_length=100) song_image = models.ImageField(upload_to=None, height_field=None, width_field=None,max_length=100)

Django one to many get all underlying records

一世执手 提交于 2021-01-29 09:18:51
问题 I'am creating a django application with a database including the following tables: I created the models as following: class Group(models.Model): id = models.AutoField(primary_key=True, editable=False) text = models.CharField(db_column='Text', max_length=4000) class Meta: db_table = 'Group' class Filters(models.Model): id = models.AutoField(primary_key=True, editable=False) text = models.CharField(db_column='Text', max_length=4000) group_id = models.ForeignKey(Group, on_delete=models.CASCADE,