Django模型引用其他类的对象 [英] django model referencing object from other class
问题描述
您好,对于django来说还很新,但是考虑到以下模型及其关系,我如何为该对象创建一个只读字段,该字段是对另一个类中字段的引用?我已经在stackoverflow上寻找了一段时间,但是不确定是什么样的模型引用.
Hi there pretty new to django but considering the below models, with their relationships, how do I create a read only field for the object that is a reference to a field in another class? I've looked for a while on stackoverflow, but not sure what kind of model reference that would be.
这的基本逻辑是:我有一个位于服务器机房地板上的服务器机架,并将其与机架位置相关联,并排成一行以管理功耗和其他物品.仅供最终用户参考,我想要一个只读字段来向他们显示此机架所处的行,以及该机架派生自机架位置.我一直在摆弄一个方法来查找它,但似乎无法弄清楚语法或在django管理页面上找到相关的内容.
The basic logic for this being: I have this server rack that sites on a floor in a server room, and I'm associating it to a rack position, and row to manage power consumption and other goodies. Just for my end-user's reference I want a read only field to show them what row this rack lives in, and its derived from the rack position. I'd been fiddling around with creating a method to look it up, but can't seem to figure out the syntax or find something related on the django admin pages.
任何想法都将受到高度赞赏,因为我一直盯着文档,我真的可以使用该帮助,而且似乎无法为此找到相关的模型参考.
Any ideas would be super appreciated, I really could use the help as I've been staring through docs forever, and can't seem to find a relevant model reference for this.
class rack(models.Model):
class Meta:
verbose_name = "Rack"
verbose_name_plural = "Racks"
def __unicode__(self):
return str(self.position)
def row(self, obj):
return self.position.row
position = models.OneToOneField("rackposition")
row = row(position.row.row)
asstag = models.CharField("Asset Tag", max_length=200, unique=True)
rackunits = models.IntegerField("Rack Units")
class rackposition(models.Model):
class Meta:
verbose_name = "Rack Position"
verbose_name_plural = "Rack Positions"
def __unicode__(self):
return str(self.position)
position = models.CharField("Position", max_length=35, primary_key=True)
row = models.ForeignKey("row")
class row(models.Model):
class Meta:
verbose_name = "Row"
verbose_name_plural = "Rows"
def __unicode__(self):
return str(self.row) + "." + str(self.suite)
row = models.CharField("Row ID", max_length=200, unique=True)
suite = models.ForeignKey(suite, blank=False)
power_budget = models.IntegerField("Power Budget")
power_volt = models.IntegerField("Power Voltage")
dual_bus = models.BooleanField("Dual Bus", default=False)
推荐答案
您不需要方法.假设您有一个名为 my_rack
的机架实例,则可以通过 my_rack.position.row
获得其行.
You don't need a method. Assuming you have a rack instance called my_rack
, you can get its row with my_rack.position.row
.
注意,您应该真正遵循PEP8,并使用CamelCase作为类名.
Note, you should really follow PEP8 and use CamelCase for your class names.
如果要在管理员中将其视为只读字段,则需要在模型或ModelAdmin类上定义一个方法.例如:
If you want to see it as a readonly field in the admin, you will need to define a method either on the model or on the ModelAdmin class. For example:
class RackAdmin(admin.ModelAdmin):
model = Rack
readonly_fields = ('row',)
def row(self, obj):
return obj.position.row
这篇关于Django模型引用其他类的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!