简体   繁体   English

在 Django 中创建一个 model 方法,该方法确定共享相同外键的所有对象的 ID

[英]In Django create a model method which determines an id for all objects that share the same foreign key

Kind of new to Django and while trying to create my first own project, i stumbled upon a problem. Django 有点新,在尝试创建我的第一个自己的项目时,我偶然发现了一个问题。 I have two different model classes: Star and Pl.net .我有两个不同的 model 类: StarPl.net Each pl.net has a foreignkey which belongs to a Star.每个 pl.net 都有一个属于一个 Star 的外键。 Now I would like to have a field for Pl.net , which is basically the id/pk for its star system that is given on creation/discovery.现在我想要一个Pl.net字段,它基本上是在创建/发现时给出的其恒星系统的 id/pk。 All known pl.nets of the star system already have their star system id and thus the newly created/discovered pl.net would require an id that is incremented on the last id that was given.所有已知的恒星系统 pl.net 都已经有了它们的恒星系统 ID,因此新创建/发现的 pl.net 将需要一个 ID,该 ID 在给出的最后一个 ID 的基础上递增。

Id ID Star星星 Id in Star System恒星系统中的ID Name名称
1 1个 Sun太阳 1 1个 Neptune海王星
2 2个 Alpha Centauri半人马座阿尔法星 1 1个 Hoth霍斯
3 3个 Alpha Centauri半人马座阿尔法星 2 2个 Coruscant
4 4个 Sun太阳 2 2个 Earth地球
5 5个 Sun太阳 3 3个 Jupiter木星
6 6个 Alpha Centauri半人马座阿尔法星 3 3个 Yavin4雅文4
7 7 Sun太阳 4 4个 Venus金星

My models are looking like this我的模型看起来像这样

models.py:模型.py:

class Star(models.Model):
   name = models.CharField(max_length=200)

class Planet(models.Model):
   name = models.CharField(max_length=200)
   star = models.ForeignKey(Star, on_delete=models.CASCADE)

   def starSystemPlanetId(self):
      What could work in here?

This new Id should be available to be called in a template like this:这个新的 Id 应该可以在这样的模板中调用:

{% for planets in object_list %}
    <li> 
         <p>Planet {{planets.name}} is the Nr. {{planets.customer_id}} planet of the star system</p>
    </li>
{% endfor %}

I have learned already, that business logic should be implemented inside models.py via a model method.我已经了解到,业务逻辑应该通过 model 方法在models.py中实现。

def starSystemPlanetId(self, star_var):
   starSystemPlanetId = Planet.objects.filter(star = star_var).filter(pk >> earlierPlanetsPk).count()
   return starSystemPlanetId

Maybe there is a standard query function available for this kind of problem already.也许已经有一个标准查询 function 可用于此类问题。

This is my suggestion (following the comments on you post):这是我的建议(根据您发表的评论):

class Planet(models.Model):
    name = models.CharField(max_length=200)
    star = models.ForeignKey(Star, on_delete=models.CASCADE, related_name='planets')

    # Use property since it's more pythonic
    @property
    def star_system_planet_id(self):
       """0-based display identifier for a planet in the start system."""
        # No need for the 'star_var' variable since you already have it in the 
        # model instance
        return self.star.planets.filter(pk__lt=self.pk).count()

Small note: the property won't work for objects that have not been stored in the DB cause they don't have the PK assigned yet.小提示:该属性不适用于尚未存储在数据库中的对象,因为它们尚未分配 PK。 For this, you probably want to return an empty string or None , etc. Up to you.为此,您可能想要返回一个空字符串或None等。由您决定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM