django-serializer

Serialize Model from Derived (Abstract) Model

寵の児 提交于 2021-01-28 19:43:17
问题 I have the following models: class Address(models.Model): first_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100) .... class Meta: abstract = True class BillingAddress(Address): is_default = models.BooleanField() ... class Meta: db_table = 'billing_address' I'm trying to build a serializer for BillingAddress: class AddressSerializer(serializers.ModelSerializer): class Meta: abstract = True model = AddressModel class BillingAddressSerializer(AddressSerializer

Database first Django models

血红的双手。 提交于 2021-01-27 06:05:30
问题 In ASP.NET there is entity framework or something called code first from database. Is there something similar for Django? I usually work with a pre-existing database that I need to create a backend (and subsequently a front end) for. Some of these RDBs have many tables and relations so manually writing models isn't a good idea. I've scoured Google for solutions but have come up relatively empty handed. Thoughts would be appreciated. Thanks! 回答1: You can use the information on this link.

How to post OneToOne field in django rest-framework using overwrite create method

落爺英雄遲暮 提交于 2021-01-05 07:31:15
问题 I am trying to override create method to make a post request but i am stuck, here is my code class Order(models.Model): street_number = models.PositiveIntegerField(blank=True, null=True) street_name = models.CharField(max_length=250, null=True, blank=True) class Seller(models.Model): order = models.OneToOneField(Order, on_delete=models.CASCADE, related_name='seller_order',blank=True, null=True) first_name = models.CharField(max_length=250, null=True, blank=True) last_name = models.CharField

How to post OneToOne field in django rest-framework using overwrite create method

允我心安 提交于 2021-01-05 07:24:24
问题 I am trying to override create method to make a post request but i am stuck, here is my code class Order(models.Model): street_number = models.PositiveIntegerField(blank=True, null=True) street_name = models.CharField(max_length=250, null=True, blank=True) class Seller(models.Model): order = models.OneToOneField(Order, on_delete=models.CASCADE, related_name='seller_order',blank=True, null=True) first_name = models.CharField(max_length=250, null=True, blank=True) last_name = models.CharField

Django Rest framework Serialize many to many field

邮差的信 提交于 2020-12-29 07:12:10
问题 I am trying to serialise a json payload that has a field with an array, the .is_valid() check is returning true but I am getting KeyError: 'passengers' when I try to do this serializer.data['passengers'] but the other fields work fine (such as booking_number and status). This is the response.data I am passing to the seralizer: {'booking_number': 2839, 'passengers': [{'first_name': 'Jack', 'surname': 'Smith', 'email': 'smith@mail.com', 'phone_number': '1234'}], 'status': 'ON_HOLD'} My

Django Rest framework Serialize many to many field

风格不统一 提交于 2020-12-29 07:10:48
问题 I am trying to serialise a json payload that has a field with an array, the .is_valid() check is returning true but I am getting KeyError: 'passengers' when I try to do this serializer.data['passengers'] but the other fields work fine (such as booking_number and status). This is the response.data I am passing to the seralizer: {'booking_number': 2839, 'passengers': [{'first_name': 'Jack', 'surname': 'Smith', 'email': 'smith@mail.com', 'phone_number': '1234'}], 'status': 'ON_HOLD'} My

Django Rest framework Serialize many to many field

爷,独闯天下 提交于 2020-12-29 07:07:31
问题 I am trying to serialise a json payload that has a field with an array, the .is_valid() check is returning true but I am getting KeyError: 'passengers' when I try to do this serializer.data['passengers'] but the other fields work fine (such as booking_number and status). This is the response.data I am passing to the seralizer: {'booking_number': 2839, 'passengers': [{'first_name': 'Jack', 'surname': 'Smith', 'email': 'smith@mail.com', 'phone_number': '1234'}], 'status': 'ON_HOLD'} My