models.py

from django.contrib.auth.models import User class Users(models.Model): name=models.CharField(max_length=150,blank=True,null=True) def __str__(self): return self.name class Person(models.Model): user = models.OneToOneField(User, blank=True, null=True) users = models.OneToOneField(Users, unique=True) name = models.CharField('Имя',max_length = 250) class Info(models.Model): person = models.OneToOneField(Person, blank=True, null=True) accountname = models.CharField('логин',max_length=128, blank=True, null=True, unique = True) 

views.py

 users = Users.objects.all() for user in users: query = Person.objects.get(info__accountname=user) query.users = Users.objects.get(id=user.id) query.save() 

In the Person and Info models, the data is loaded from the external database. The Users model is loaded from the json file, in this case we are only interested in the name field. In the views file, we iterate over all users and compare with the accountname field. If they are equal then tie. Users become attached, but at the same time an error pops up:

 column users_id is not unique 

    1 answer 1

    Somehow all is confusing and not enough data. First, here is this line:

     query.users = Users.objects.get(id=user.id) 

    It looks more correct like this:

     users = Users.objects.all() for user in users: query = Person.objects.get(info__accountname=user) query.users = user query.save() 

    Let's rename and simplify something for clarity:

     for users_item in Users.objects.all(): person = Person.objects.get(info__accountname=users_item.name) person.users = users_item person.save() 

    Where you relied on an implicit user-to-string conversion, it is better to explicitly specify a field or make an explicit type conversion.

    Now what could have caused your mistake. In the Users model, there is no requirement for the uniqueness of the name field, which means that there can be more than one record that will be matched with different persons.