Django Admin--根据日期显示选择--parking_on

时间:2018-09-21 08:50:20

标签: django django-admin

我想在Django管理员中实现一个仅显示可用停车位的功能。

今天在我的数据库中,只有2个停车位被占用(P1和P6),因此我只想向用户显示他要预订该位的当天空闲的空间==>归档文件的名称是parking_on。如果所有的都是书,那么有个消息“今天没有运气! enter image description here enter image description here 我的模型如下:

class ParcareManager(models.Manager):

    def active(self, *args, **kwargs):
        return super(ParcareManager, self).filter(draft=False).filter(parking_on__lte=timezone.now())

class Parcare(models.Model):
    PARKING_PLOT = (
        ('P1', 'Parking #1'),
        ('P2', 'Parking #2'),
        ('P3', 'Parking #3'),
        ('P4', 'Parking #4'),
        ('P5', 'Parking #5'),
        ('P6', 'Parking #6'),
        ('P7', 'Parking #7'),
        ('P8', 'Parking #8'),
        ('P9', 'Parking #9'),
        ('P10', 'Parking #10'),
        ('P11', 'Parking #11'),
        ('P12', 'Parking #12'),
        ('P13', 'Parking #13'),
        ('P14', 'Parking #14'),
        ('P15', 'Parking #15'),

    )
    user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, default=1, on_delete=True)
    # name = models.CharField(max_length=120,blank=True, null=True)
    # slug = models.SlugField(unique=True)

    email=models.EmailField(blank=True, null=True)
    # email = models.EmailField(settings.AUTH_USER_MODEL)
    parking_on = models.DateField(auto_now=False, auto_now_add=False,blank=True, null=True)
    parking_off = models.DateField(
        auto_now=False, auto_now_add=False, blank=True, null=True) #plecarea din parcare
    numar_masina=models.CharField(max_length=8, default="B100AAA", blank=True, null=True)
    location =models.CharField(max_length=3, default="P1", blank=True, null=True, choices=PARKING_PLOT)
    updated = models.DateTimeField(auto_now=True, auto_now_add=False,blank=True, null=True)
    timestamp=models.DateTimeField(auto_now=False, auto_now_add=True,blank=True, null=True)
    venire = models.TimeField(default=datetime.time(9, 00),auto_now=False, auto_now_add=False )
    plecare = models.TimeField(default=datetime.time(
        18, 00), auto_now=False, auto_now_add=False)


    objects = ParcareManager()

    def __str__(self):
        return self.email + " | " + self.location + " | " + str(self.parking_on) + " | " + str(self.parking_off)

    class Meta:
        verbose_name_plural = "parcare"

class Meta:
    ordering = ["-timestamp", "-updated"]

我的管理员看起来像这样:

from django.contrib import admin

# Register your models here.
from .models import Parcare


class ParcareModelAdmin(admin.ModelAdmin):
    list_display = [ "email","user", "location",
                    "parking_on", "parking_off",  "venire", "plecare"]
    list_display_links = ["email", "user" ]
    list_editable = [ "parking_off", "parking_on","venire", "plecare"]
    list_filter = ["parking_on", "parking_off"]
    search_fields = ["location", "name"]

    class Meta:
        model = Parcare

    # def email(self, obj):
    #     return obj.user.email

    def get_form(self, request, obj=None, **kwargs):
        form = super().get_form(request, obj, **kwargs)
        if not obj:
            user = request.user
            form.base_fields['user'].initial = user
            form.base_fields['email'].initial = user.email
        return form

admin.site.register(Parcare, ParcareModelAdmin)

0 个答案:

没有答案