Я пытаюсь получить информацию о моем клиенте, но у меня возникают проблемы с его фильтрацией по внешнему ключу из внешнего ключа. Вот что у меня сейчас:
Сериализаторы:
class CustomerOrderSerializer(serializers.ModelSerializer):
customer = OrderCustomerSerializer()
class Meta:
model = CustomerDetails
fields = ('id', 'current_selfie', 'customer', 'phone')
class OrderSerializer(serializers.ModelSerializer):
customer= OrderCustomerSerializer()
driver = OrderDriverSerializer()
dispensary = OrderDispensarySerializer()
order_details = OrderDetailSerializer(many=True)
status = serializers.ReadOnlyField(source="get_status_display")
customer_details = CustomerOrderSerializer.OneToOneField(customer, related_name='customer_details')
class Meta:
model = Order
fields = ("id", "customer", "customer_details", "dispensary", 'delivery_signature', 'order_rated',
"driver", "order_details", "total", "status", "address", "created_at")
Модель
class Order(models.Model):
a = 1
b = 2
c = 3
d = 4
e = 5
STATUS_CHOICES = (
(a, "ah"),
(b, "bb"),
(c, "cc"),
(d, "dd"),
(CANCELED, "Canceled/Refunded")
)
address = models.CharField(max_length=500)
created_at = models.DateTimeField(default=timezone.now)
customer = models.ForeignKey(CustomerDetails, on_delete=models.CASCADE)
delivery_signature = models.ImageField(
upload_to='delivery_selfie/', blank=True, default='')
delivered_at = models.DateTimeField(default=timezone.now)
dispensary = models.ForeignKey(Dispensary, on_delete=models.CASCADE)
driver = models.ForeignKey(
Driver, blank=True, null=True, on_delete=models.CASCADE)
picked_at = models.DateTimeField(default=timezone.now)
order_rated = models.BooleanField(default=False)
status = models.IntegerField(choices=STATUS_CHOICES)
total = models.FloatField()
def __str__(self):
return str(self.id)
Класс CustomerDetails (models.Model):
age = models.IntegerField(default="21", blank=True)
address = models.CharField(
default='', max_length=254, null=True, blank=True)
nick_name = models.CharField(max_length=500, blank=True)
average_order = models.FloatField(default="0.0", blank=True)
completed_orders = models.IntegerField(default="0", blank=True)
customer = models.ForeignKey(
Customer, on_delete=models.CASCADE, related_name='customer')
customer_type = MultiSelectField(
choices=CUSTYPE, default=CUSTYPE, max_length=100)
current_selfie = models.ImageField(
upload_to='sefies/', blank=True, default='')
email_confirmed = models.BooleanField(default=False)
last_signin = models.DateTimeField(default=timezone.now)
phone = models.CharField(
blank=True, help_text='Contact phone number', max_length=20)
signup = models.DateTimeField(default=timezone.now)
wants_to_feel = MultiSelectField(
max_length=100, choices=FEELINGS, blank=True)
@property
def customer_name(self):
return (self.customer.name)
def __str__(self):
return self.customer.user.get_full_name()
Мне нужно вернуть данные клиента, когда вызывается API, но я не уверен, как сортировать по клиенту, чтобы вернуть данные клиента.