如何在django中获取相关模型的字段值

时间:2017-07-09 14:08:42

标签: python django django-models relational-database

我想让start_dateend_date范围之间的用户性别在models.py

 class Profile(models.Model):
    MALE = 'M'
    FEMALE = 'F'
    GENDER_CHOICES = (
        (MALE, u'ere'),
        (FEMALE, u'eme')
    )

    FOOD_TYPE = (
        ('Veg', 'Vegeterian'),
        ('Green', 'Green')
    )
    user = models.OneToOneField(User, related_name="profile", null=True)
    firstname = models.CharField(max_length=50, null=True)
    lastname = models.CharField(max_length=50, null=True)
    phone1 = models.CharField(max_length=50, null=True)
    phone2 = models.CharField(max_length=50, null=True, blank=True)
    birthdate = models.DateField(blank=True, null=True)
    gender = models.CharField(max_length=10, choices=GENDER_CHOICES, null=True)
    emd_number = models.CharField(max_length=10, null=True)
    special_situation = models.IntegerField(choices=SITUATION_CHOICES, null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    email = models.EmailField(blank=True, null=True)
    city = models.CharField(max_length=20, blank=True)
    food_type = models.CharField(max_length=50, choices=FOOD_TYPE, blank=True)

    def __unicode__(self):
        return self.user.username


class Room(models.Model):
    number = models.CharField(max_length=8, null=True)
    category = models.ForeignKey(RoomCategory, related_name="rooms")
    beds = models.PositiveIntegerField(null=True)
    description = models.TextField(blank=True)
    size = models.CharField(max_length=2, blank=True)

    def __unicode__(self):
        return self.number

class Reservation(models.Model):
    created_at = models.DateTimeField(auto_now_add=True, editable=False)
    transport_type = models.CharField(max_length=8, blank=True)
    start_date = models.DateField(null=True)
    end_date = models.DateField(null=True)
    total_days = models.PositiveIntegerField(null=True, blank=True)
    arrival_date = models.DateField(null=True, blank=True)
    user = models.ForeignKey(User, related_name="customers")
    room = models.ForeignKey(Room, related_name="reservations")
    total_payment = models.PositiveIntegerField(null=True, blank=True)
    total_paid = models.PositiveIntegerField(null=True, blank=True)

目前我通过以下查询获得房间:

queryset = Room.objects.all()
if start_date && end_date:
    queryset = queryset.exclude(reservations__start_date__range=[start_date, end_date], reservations__end_date__range=[start_date, end_date])

如何获取日期之间的用户性别

2 个答案:

答案 0 :(得分:0)

你可以查一下,

for reservetion in Reservation.objects.filter(room__in=queryset):
     print reservation.user.gender

答案 1 :(得分:0)

@Adaikalaraj如何在get_queryset方法中附加用户性别?

class RoomList(ListAPIView):
    """ Listing all room """
    serializer_class = RoomSerializer

    def get_queryset(self):
        queryset = Room.objects.all()
        start_date = self.request.query_params.get('start_date', None) #date string
        end_date = self.request.query_params.get('end_date', None)  #date string
        if start_date and end_date:
            queryset = queryset.exclude(reservations__start_date__range=[start_date, end_date], reservations__end_date__range=[start_date, end_date])
        return queryset