django admin外键默认值内联

django admin外键默认值内联,django,django-admin,foreign-keys,inline,django-grappelli,Django,Django Admin,Foreign Keys,Inline,Django Grappelli,我正在将一个古老的客户机/服务器应用程序(Delphi)转换成一个Django应用程序,用于一个小型砖块和灰泥书店(妻子书店) 因为大多数函数都是admin,所以我使用带有grappelli的Django admin接口来进行一些更简单的查找 我有三种型号:书籍、销售和商品 class Book(TimeStampedModel): """ Books are described individually and are related to collections as

我正在将一个古老的客户机/服务器应用程序(Delphi)转换成一个Django应用程序,用于一个小型砖块和灰泥书店(妻子书店)

因为大多数函数都是admin,所以我使用带有grappelli的Django admin接口来进行一些更简单的查找

我有三种型号:书籍、销售和商品

class Book(TimeStampedModel):
    """
    Books are described individually and are related to collections
    as many to many. Every book in this system is unique - i.e. there are 
    not quantity fields. This is optimized for used book stores where book
    condition is essential.
    """

    STATUS_CHOICES = (
                        ('IN STOCK', 'IN STOCK'), 
                        ('SOLD', 'SOLD'),
                        ('ON LOAN', 'ON LOAN'),
                        ('HOLD', 'HOLD'),
                     )

    isbn = models.CharField(max_length=50, blank=True)
    title = models.CharField(max_length=200, blank=False, db_index=True)
    author = models.CharField(max_length=200, blank=True)
    sell_price = models.DecimalField(max_digits=10,decimal_places=2, default=0)
    description = models.TextField()

    collections = models.ManyToManyField(Collection)

    class Meta:
        index_together = [
            ["author", "title"],
            ["status", "title", "author"],
        ]

    def __unicode__(self):
        return "%s [%d] - %s - $%.2f" % (self.title, self.id, self.book_type, self.sell_price)

    @staticmethod
    def autocomplete_queryset():
        instock = Book.objects.filter(status="IN STOCK")
        return instock    

    @staticmethod
    def autocomplete_search_fields():
        return("id__iexact", "title__istartswith",)

class Sale(TimeStampedModel):
    """ 
    Sales group all sold items which may or may not be books and are sold to contacts.  
    We use a "generic" contact of "cash" for non named contacts 
    """ 
    PAYMENT_TYPE_CHOICES =  ( ('Cash', 'Cash'), ('Charge', 'Charge'), ('Check', 'Check'))

    contact = models.ForeignKey(Contact, null=True)
    sale_date = models.DateField(blank=True,default=datetime.date.today, db_index=True)
    payment_type = models.CharField(max_length=50, choices=PAYMENT_TYPE_CHOICES)
    taxed = models.BooleanField(default=True)
    tax_exempt_no = models.CharField(blank=True, max_length=50)
    sales_tax = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
    amt_tender = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
    pct_discount = models.SmallIntegerField(blank=True, null=True)
    amt_credit = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
    amt_shipping = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
    amt_due = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
    tot_sale = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
    tot_items = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)

    ordering = ['-sale_date']

    def __unicode__(self):
        return str(self.sale_date)

class Item(TimeStampedModel):
    """
    Items are usually books sold on a sale. Items can also be entered manually
    at time of sale if they are not books from inventory
    """
    sale = models.ForeignKey(Sale)
    book = models.ForeignKey(Book, null=True, blank=True)
    item_desc = models.CharField(blank=True, max_length=200)
    cost = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
    sell_price = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)

    def __unicode__(self):
        return self.item_desc
对于销售表单,我使用带有表格内联项的管理表单。项目通常是书籍(通过外键查找),但也可以手动输入非库存项目,因此我在书籍模型和项目模型中都有售价

class ItemInline(admin.TabularInline):
    model = Item
    raw_id_fields = ("book",)
    autocomplete_lookup_fields = {
        'fk': ['book'],
    }
    extra = 2
在外键查找中,我要做的是返回书籍的键,并用我查找的书籍中的sellprice填写物品的sellprice

我的基本查找工作正常,但找不到如何在查找后立即将项目sellprice设置为书籍的sellprice


任何建议都将不胜感激!我已经试着找出要放入一些JS逻辑的对象,但我认为内联线是动态创建对象ID的。我不是JS专家。

请将相关代码添加到您的帖子中,以便我们可以帮助您。使用源代码更新。我简化了一点。还在为这件事挣扎。