django admin外键默认值内联

时间:2014-02-12 00:06:18

标签: django django-admin foreign-keys inline django-grappelli

我将古老的客户端/服务器应用程序(Delphi)转换为Django应用程序,用于小型砖和书店(妻子)。

由于大多数功能都是管理员,我使用Django管理界面和grappelli进行一些更容易的查找。

我有3种型号:书籍,促销和物品。

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

对于Sale表单,我使用带有Tabular内联项的管理表单。项目通常是书籍(通过外键查找),但也可以手动输入非库存项目,因此我在书籍模型和项目模型中都有一个sell_price。

class ItemInline(admin.TabularInline):
    model = Item
    raw_id_fields = ("book",)
    autocomplete_lookup_fields = {
        'fk': ['book'],
    }
    extra = 2

我要在外键查找中做的是返回书的密钥并使用我查找的书中的售价填写Item's sellprice。

我的基本查询工作正常,但无法在查找后立即了解如何将商品售价设置为图书的卖价。

任何建议表示赞赏!我想我已经尝试找出对象来放置一些JS逻辑,但内联得到了动态创建的对象id。我不是JS专家。

0 个答案:

没有答案