Django模型引用其他类的对象

编程入门 行业动态 更新时间:2024-10-24 20:14:04
本文介绍了Django模型引用其他类的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好,对于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模型引用其他类的对象

本文发布于:2023-11-02 13:54:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552578.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   对象   Django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!