数学和更改字段颜色

发布于 2024-08-18 22:40:34 字数 1083 浏览 1 评论 0原文

from django.db import models
from django.contrib.auth.models import User

class Product(models.Model):
name = models.CharField(max_length = 127)
description = models.TextField()
code = models.CharField(max_length = 30)
lot_no = models.CharField(max_length = 30)
inventory = models.IntegerField()
commited = models.IntegerField()
reorder = models.IntegerField()
created_date = models.DateField(auto_now_add = True)
comment_user = models.ForeignKey(User, null=True)
comment_txt = models.TextField()

def __unicode__(self):
    return self.code + " - " + self.name + " - " + self.lot_no + " - " + str(self.created_date)

@property
def available(self):
    return self.inventory - self.commited

admin.py

from django.contrib import admin
from CMS.Inventory.models import Product

class padmin(admin.ModelAdmin):
search_fields=['name', 'description', 'code', 'lot_no' ]
admin.site.register(Product, padmin)

我试图创建一个不可编辑但在 django 产品类中可见的字段,该类在 @property 字段中执行数学运算。我还尝试对可重新订购的产品进行比较,如果重新订购的数量少于可用的数量,则重新订购字段将变为红色,表明您需要订购更多。谢谢

from django.db import models
from django.contrib.auth.models import User

class Product(models.Model):
name = models.CharField(max_length = 127)
description = models.TextField()
code = models.CharField(max_length = 30)
lot_no = models.CharField(max_length = 30)
inventory = models.IntegerField()
commited = models.IntegerField()
reorder = models.IntegerField()
created_date = models.DateField(auto_now_add = True)
comment_user = models.ForeignKey(User, null=True)
comment_txt = models.TextField()

def __unicode__(self):
    return self.code + " - " + self.name + " - " + self.lot_no + " - " + str(self.created_date)

@property
def available(self):
    return self.inventory - self.commited

admin.py

from django.contrib import admin
from CMS.Inventory.models import Product

class padmin(admin.ModelAdmin):
search_fields=['name', 'description', 'code', 'lot_no' ]
admin.site.register(Product, padmin)

I am trying to make a field that is non editable but viewable in the django product class that does the math thats in the @property field. I am also trying to do a comparison of available to reorder such that if reorder is less than available the reorder field will become red showing that you needed to order more. Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长伴 2024-08-25 22:40:34
def colored_available(self):
    if self.reorder < self.available:
       return self.reorder
    return '<span style="color: red;">%d</span>' % self.reorder

colored_available.allow_tags = True
colored_available.short_description = "Available"

将此信息发送给您的模型管理员 (padmin):

list_display = ('name', 'colored_available') # add more fields to be displayed in the list of products
readonly_fields = ('colored_available',)
def colored_available(self):
    if self.reorder < self.available:
       return self.reorder
    return '<span style="color: red;">%d</span>' % self.reorder

colored_available.allow_tags = True
colored_available.short_description = "Available"

And this to your model admin (padmin):

list_display = ('name', 'colored_available') # add more fields to be displayed in the list of products
readonly_fields = ('colored_available',)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文