开发者

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

开发者 https://www.devze.com 2023-02-01 03:40 出处:网络
I want to ha开发者_如何学JAVAve 2 Foreign Keys for an Inline form in the admin. I have a Timesheet model with the following fields:

I want to ha开发者_如何学JAVAve 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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号