简体   繁体   中英

Two Foreign keys for an Inline form in the admin in Django

I want to have 2 Foreign Keys for an Inline form in the admin. I have a Timesheet model with the following fields:

class Timesheet(models.Model):
    date = models.ForeignKey(DateTimesheet, related_name="day")
    supervisor = models.ForeignKey(DateTimesheet, related_name="superintendent")
    job = models.ForeignKey(Job)
    phase = models.ForeignKey(Phase)
    equip = models.ForeignKey(Equipment, null=True, blank=True)
    employee = models.ForeignKey(Employee)
    local = models.ForeignKey(Local)
    pay_class = models.ForeignKey(PayClass)
    reg = models.IntegerField(max_length=1)
    ot = models.IntegerField(max_length=2, null=True, blank=True)
    bill_rate = models.DecimalField(decimal_places=2,max_digits=6,blank=True,null=True)
    bill_hours = models.IntegerField(max_length=2,blank=True,null=True,)

and a DateTimesheet with these Fields:

class DateTimesheet(models.Model):
    date = models.DateField()
    supervisor = models.ForeignKey(User)

I want to be able to select date & supervisor and then have the rest of the timesheet fields inline. It seems that I need a composite key and from what I have read Django does not have composite keys. Is this at all possible to do or am I out of luck?

If Timesheet is the parent model and DateTimesheet the inline (or vice-versa), there is no need for composite keys, just:

  1. make the supervisor in both Models a ForeignKey(User)
  2. ommit the supervisor field in the inline form
  3. set it to the vaule of parent model field when saving by overriding the save_formset method .

Remember:

InlineModelAdmin shares many of the same features as ModelAdmin, and adds some of its own (the shared features are actually defined in the BaseModelAdmin superclass).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM