模型中的多个外键(一对多映射)Django Python?

时间:2018-08-27 23:00:45

标签: python django

我正在尝试绘制体育馆模型与其报价和事件模型之间的关系。这个想法是1个健身房可以提供很多优惠和很多活动。但是1个报价不能属于许多体育馆,而1个事件不能属于许多体育馆。因此,从外观上看,这是一对多的关系。到目前为止,一切都很好。这是我第一次使用Django ORM,因此我的方法可能有点天真。我实际上希望在数据库中有一些表,例如健身房表,活动表和要约表。报价和活动表通过健身房ID连接到健身房。我该如何实现?因为到目前为止,我在Django管理页面中看到了一些非常奇怪的东西。这就是我到目前为止所尝试的

class Program(models.Model):
    id = models.AutoField(primary_key=True)
    created = models.DateTimeField(auto_now_add=True, editable=False)
    last_updated = models.DateTimeField(auto_now=True, editable=False)
    name = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=6, decimal_places=2)

    class Meta:
        ordering = ('last_updated',)


    def __str__(self):
        return "%s %s" % (self.id, self.price)


class Offer(models.Model):
    id = models.AutoField(primary_key=True)
    created = models.DateTimeField(auto_now_add=True, editable=False)
    last_updated = models.DateTimeField(auto_now=True, editable=False)
    name = models.CharField(max_length=100)
    numberofsessions = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=6, decimal_places=2)
    expireddate = models.DateTimeField(auto_now_add=True, editable=False)

    class Meta:
        ordering = ('last_updated',)


    def __str__(self):
        return "%s %s" % (self.id, self.expireddate)


class Event(models.Model):
    id = models.AutoField(primary_key=True)
    created = models.DateTimeField(auto_now_add=True, editable=False)
    last_updated = models.DateTimeField(auto_now=True, editable=False)
    name = models.CharField(max_length=100)
    location = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=6, decimal_places=2)
    eventdate = models.DateTimeField(auto_now_add=True, editable=False)

    class Meta:
        ordering = ('last_updated',)


    def __str__(self):
        return "%s %s" % (self.id, self.eventdate)


class User(models.Model):
    id = models.AutoField(primary_key=True)
    created = models.DateTimeField(auto_now_add=True, editable=False)
    last_updated = models.DateTimeField(auto_now=True, editable=False)
    name = models.CharField(max_length=100)
    phonenumber = models.CharField(max_length=100)
    amount = models.DecimalField(max_digits=6, decimal_places=2)
    contact = models.CharField(max_length=100)
    email = models.EmailField(max_length=70)
    monthpaid = models.CharField(max_length=100)

    class Meta:
        ordering = ('last_updated',)


    def __str__(self):
        return "%s %s %s" % (self.id, self.phonenumber,self.monthpaid)


class Gym(models.Model):
    # Fields
    program = models.ForeignKey(Program)
    event = models.ForeignKey(Event)
    offer = models.ForeignKey(Offer)
    event = models.ForeignKey(Event)
    user = models.ForeignKey(User)
    id = models.AutoField(primary_key=True)
    created = models.DateTimeField(auto_now_add=True, editable=False)
    last_updated = models.DateTimeField(auto_now=True, editable=False)
    name = models.CharField(max_length=100)
    email = models.CharField(max_length=100, null=True, blank=True)
    phonenumber = models.CharField(max_length=100)
    website = models.CharField(max_length=100, null=True, blank=True)
    location = models.CharField(max_length=500)
    zipcode = models.CharField(max_length=100, null=True, blank=True)
    contactperson = models.CharField(max_length=100)

    class Meta:
        ordering = ('last_updated',)

    def __str__(self):
        return "%s %s" % (self.id, self.name)

admin.py
from django.contrib import admin
from .models import Gym, Event, Offer, Program, User


class Gyminline(admin.TabularInline):
    model = Gym


class EventAdmin(admin.ModelAdmin):
    inlines = (Gyminline,)

admin.site.register(Event, EventAdmin)


class OfferAdmin(admin.ModelAdmin):
    inlines = (Gyminline,)



admin.site.register(Offer, OfferAdmin)


class ProgramAdmin(admin.ModelAdmin):
    inlines = (Gyminline,)



admin.site.register(Program, ProgramAdmin)


class UserAdmin(admin.ModelAdmin):
    inlines = (Gyminline,)


admin.site.register(User, UserAdmin)

0 个答案:

没有答案