简体   繁体   中英

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. Here Emp_position is that relationship 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. I was able to get the user model instance easily using the user.username field but how do I get instance of position. The position instance, I am deriving using some logic by using the filter function. How do I get the instance which function helps me in getting the instance using some condition.

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.

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. 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 .

Note: An instance of any model can be get by using filter and then using the 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. 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. Instead use get.

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

emp_pos.save()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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