简体   繁体   English

获取 model 的实例以用作外键以将数据添加到 Django 中的其他 model

[英]Get instance of a model to be used as foreign key to add data to other model in Django

I have a relationship model that maps two models that is my user and its position as mentioned.我有一个关系 model 映射两个模型,即我的用户和提到的position Here Emp_position is that relationship model.这里Emp_position是关系 model。

class Position(models.Model):
    position_name = models.CharField(max_length=20, unique=True)
    def __str__(self):
        return self.position_name


class Emp_position(models.Model):

    emp_uname = models.OneToOneField(User, related_name='emp_name', to_field='username', on_delete=models.CASCADE)
    position_name = models.ForeignKey(Position, related_name='position', to_field='position_name', on_delete=models.CASCADE)

    def __str__(self):
        return str(self.emp_uname) + " " + str(self.position_name)

Now to insert data to Emp_position I need instance of user and Position.现在要将数据插入 Emp_position,我需要用户实例和 Position。 I was able to get the user model instance easily using the user.username field but how do I get instance of position.我能够使用user.username字段轻松获取用户 model 实例,但如何获取 position 实例。 The position instance, I am deriving using some logic by using the filter function. position 实例,我通过使用过滤器 function 使用一些逻辑推导。 How do I get the instance which function helps me in getting the instance using some condition.如何获取 function 帮助我使用某些条件获取实例的实例。

Here is what I have tried:这是我尝试过的:

emp_pos = Emp_position(emp_uname = user, position_name = Position.objects.filter(position_name="COMES FROM LOGIC").first())
emp_pos.save()

But this is not saving the model.但这并不能保存 model。

EDIT: As asked in comments section编辑:如评论部分所述

emp_pos = Emp_position(emp_uname = user, position_name = Position.objects.filter(position_name=NewProvisionalEmployeeMail.objects.filter(email="bhatnagarpulkitpb@gmail.com").values_list('position_name')).first())
emp_pos.save() 

Whatever you are doing ie filtering the database will give you a query set, you need the instance.无论您在做什么,即过滤数据库都会给您一个查询集,您需要该实例。 You have two filters then you must also have two first() to get the instance.您有两个过滤器,那么您还必须有两个 first() 才能获取实例。 Do this.做这个。

emp_pos = Emp_position(emp_uname = user, position_name = Position.objects.filter(position_name=Position.objects.filter(position_name="COMES FROM LOGIC THAT DERIVES THE FIELD NAME").first()[0]).first())
emp_pos.save()

the first first gets the first instance for second filter and the second first is for the first filter .第一个first获取第二个filter的第一个实例,第二个 first 用于第一个filter

Note: An instance of any model can be get by using filter and then using the first() function.注意:任何 model 的实例都可以通过使用过滤器然后使用 first() function 获得。 These are very helpful in populating the relationship table or to also specify our foreign key as it needs instance of our another model to which we are linking.这些对于填充关系表或指定我们的外键非常有帮助,因为它需要我们链接到的另一个 model 的实例。 If there is any other way can someone please point out in comments.如果还有其他方法可以有人在评论中指出。

When doing a model.objects.filter this will return all items that match that query, but I see that your model Position has 1 field with unique = True which means filter is not needed it.在执行model.objects.filter时,这将返回与该查询匹配的所有项目,但我看到您的 model Position有 1 个字段,不需要唯一 = True这意味着它。 Instead use get.而是使用 get。

emp_pos = Emp_position(emp_uname = user, position_name = Position.objects.get(position_name="COMES FROM LOGIC")) emp_pos = Emp_position(emp_uname = 用户,position_name = Position.objects.get(position_name="COMES FROM LOGIC"))

emp_pos.save() emp_pos.save()

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

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