How to access generated table fields in Django manytomanyfield

六月ゝ 毕业季﹏ 提交于 2020-02-25 04:32:27

问题


py which looks like below

class Scenes(models.Model):
    name = models.CharField(max_length=30)

    def __str__(self):
        return self.name

class Tech(models.Model):
    tech = models.CharField(max_length=30)
    scenario = models.ManyToManyField('Scenes')

    def __str__(self):
        return self.tech

class UserLog(models.Model):
     tech_id = models.ForeignKey(Tech, on_delete=models.CASCADE)
     ....#other fields

It generates one extra table called pages_tech_scenario (pages is my app name)

My populated tables in database (postgres) looks like below

pages_tech_scenario

 id | tech_id | scenes_id
----+---------+-----------
  1 |       1 |         1
  4 |       2 |         1
  5 |       2 |         2
  6 |       2 |         3
  7 |       2 |         4
  8 |       2 |         5
  9 |       3 |         3
 10 |       4 |         1
 11 |       4 |         2
 12 |       4 |         3

pages_scenes

 id |   name
----+----------
  1 | poweron
  2 | poweroff
  3 | sleep
  4 | wakeup
  5 | switch

pages_tech

id | tech
----+------
  2 | CDMA
  3 | GSM
  4 | 5G
  1 | LTE

pages_userlog

id |                    data_location                     |   pc    | username | target | data_type | description |   status    |    submission_time    | upload_now | string_submission_time | tech_id_id
----+------------------------------------------------------+---------+----------+--------+-----------+-------------+-------------+-----------------------+------------+------------------------+------------
 39 | C:\My Files\testFolder                               | lab0128 | ananagra | SDX55  | crash     |             | In Progress | 2020_02_12_T_17_45_04 | f          |                        |          2
 19 | C:\My Files\testFolder                               | lab0034 | sujaraj  | SDX55  | crash     |             | In Progress | 2020_02_12_T_15_46_59 | f          | 111                    |          1

Here, for pages_userlog first entry id=39 has tech_id_id is 2 which is equivalent to CDMA. While entering the form, I had selected one of the scenario (say sleep) field of CDMA (as CDMA points to all 5 scenarios, can be seen from pages_tech_scenario table).

How can I retrieve only sleep scenario from CDMA instead of all the scenarios its pointing to?.

How to get the id from pages_tech_scenario by specifying tech_id and scenes_id?.


回答1:


You can name the field from UserLog:

 tech = models.ForeignKey(Tech, on_delete=models.CASCADE)

This way you can reference the id by tech_id instead of tech_id_id

How can I retrieve only sleep scenario from CDMA instead of all the scenarios its pointing to?.

UserLog.objects.get(pk=1).tech.scenario.get(name="sleep")

How to get the id from pages_tech_scenario by specifying tech_id and scenes_id?.

Tech.scenario.through.objects.get(tech_id=tech_id, scenes_id=scenes_id).id


来源:https://stackoverflow.com/questions/60200787/how-to-access-generated-table-fields-in-django-manytomanyfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!