如何获取外键模型的所有字段

时间:2021-06-28 09:00:50

标签: django-models django-views

通过提问和阅读文档自学。目前我正在解决如何获取外键模型的所有字段。

我的示例模型如下

products/models.py

class CommonInfo(models.Model):
    name = models.CharField(max_length=200)
    address = models.CharField(max_length=200)
    city = models.CharField(max_length=20)
    pin_code = models.CharField(max_length=15)
    country = CountryField()
    email = models.EmailField()

    class Meta:
        abstract = True
        ordering = ['name']

class Accommodation(CommonInfo):
    phone = PhoneNumberField()
    description = models.TextField(max_length=3000)

我正在使用从产品应用到运营应用的住宿模式

operations/models.py

from products.models import *


class Add_Accommodation(models.Model):
    CURRENCY_CHOICES = (
        ('USD', '$'),
        ('EUR', '€'),
        ('GBP', '£'),
        ('RUP', 'Rs'),
    )
    ROOM_CHOICES = (
        ('SIN', 'Standard Single Room'),
        ('SDL', 'Standard Double Room'),
        ('TWN', 'Standard Twin Room'),
        ('TRP', 'Standard Triple Room'),
        ('DSL', 'Deluxe Single Room'),
        ('DDL', 'Deluxe Double Room'),
        ('DTW', 'Deluxe Twin Room'),
        ('DTR', 'Deluxe Triple Room'),
        ('SSL', 'Superior Single Room'),
        ('SDL', 'Superior Double Room'),
        ('STW', 'Superior Twin Room'),
        ('STR', 'Superior Triple Room'),
        ('JNR', 'Junior Suite Room'),
        ('SUT', 'Suite Room'),
        ('ONE', 'One Bedroom'),
        ('TWO', 'Two Bedroom'),
        ('THR', 'Three Bedroom'),
        ('STD', 'Studio Apartment'),
    )
    BOARD_CHOICES = (
        ('RM', 'Room Only'),
        ('BB', 'Bed & Breakfast'),
        ('HB', 'Half Board Basis'),
        ('FB', 'Full Board Basis'),
        ('AI', 'All Inclusive'),
    )
    customer = models.ForeignKey(Create_File, on_delete=models.CASCADE, related_name="%(app_label)s_%(class)s_related",
                                 related_query_name="%(app_label)s_%(class)ss")
    accommodation = models.ForeignKey(Accommodation, on_delete=models.CASCADE, related_name="%(app_label)s_%(class)s_related",
                              related_query_name="%(app_label)s_%(class)ss")
    check_in = models.DateField()
    check_out = models.DateField()
    room_type = models.CharField(max_length=30, choices=ROOM_CHOICES)
    board_basis = models.CharField(max_length=30, choices=BOARD_CHOICES)
    confirmation_number = models.CharField(max_length=40, blank=True)
    currency = models.CharField(max_length=10, choices=CURRENCY_CHOICES)
    cost = models.FloatField()
    provider = models.ForeignKey(Supplier, on_delete=models.CASCADE, related_name="%(app_label)s_%(class)s_related",
                                 related_query_name="%(app_label)s_%(class)ss")

    def __str__(self):
        return f'{self.customer} - {self.accommodation}'

我希望此 Add_Accommodation 在我的视图中显示 Accommodation 模型的所有项目,然后显示为模板。这是怎么做到的。 谢谢

0 个答案:

没有答案