2

I'm creating a population script, from an MysqlDatabase to Django Models.
Im looping through my receiving data from the Mysql and thats going good.

Now I have to write it to the Django Database...

mdl, succeeded = models.User.objects.get_or_create(
    name=name,
    password=password,
    email=email
)

I'm printing succeeded so I can see what the feedback is, but all it gives me is False

My Django Model User is edited so all fields can be blank and allows NULL or have a default

My Model User:

username = models.CharField(max_length=20, blank=True, null=True)
slug = models.SlugField(null=True, blank=True)
email = models.EmailField(unique=True, null=True)
name = models.CharField("First and last name", max_length=100)
uses_metric = models.BooleanField(default=True)
position = models.CharField("Position", max_length=70, blank=True, null=True,)
utility = models.ForeignKey(Utility, default=1, blank=True, null=True)
supplier = models.ForeignKey(Supplier, default=1, blank=True, null=True)
currency = models.ForeignKey(Currency, default=1)
phone = models.CharField("Direct phone number", max_length=40, blank=True,  default='+')
gets_notifications_on_reply = models.BooleanField("Receive notifications", default=False)
memberlist = models.BooleanField("Show member in memberslist", default=True)
registration_date = models.IntegerField(default=floor(time.time()), blank=True, null=True)
is_staff = models.BooleanField(
    _('staff status'),
    default=False,
    help_text=_('Designates whether the user can log into this site.'),
)
is_active = models.BooleanField(
    _('active'),
    default=True,
    help_text=_(
        'Designates whether this user should be treated as active. '
        'Deselect this instead of deleting accounts.'
    ),
)
USERNAME_FIELD = 'email'

1 Answer 1

5

Please check your database. This means that the User objects you are querying from there already exists. This might be due to the object already existing or alternatively your code not using the correct database. Since migrations, often use multiple databases this might be a problem as well.

According to the Django documentation, get_or_create() does not return an object and a status succeeded but instead a flag created, which indicates if this object has been newly created or already existed in the database. If it already existed, the created flag (succeeded in your code) is False.

If you want to make sure that the error is not due to objects already existing, take one data pair for which created is false and try to retrieve it using the model's get() method. If this throws a DoesNotExist error then something else is the problem. If it returns an object then that object already existed.

Sign up to request clarification or add additional context in comments.

5 Comments

+ I've tried created as well, did exactly the same. :(
Renaming the flag will not change that it has the same information. Please check again that you really removed all new data or try with different values. Is it always returning False or just on some requests? It might also be that you are trying to import the same data twice. You could also check that by using the same data to just get() a user. If this succeeds in returning you a user, then it existed before.
@ChickenWing1996, I quickly also added the hint about get() into my answer to collect all relevant information up there as well. Feel free to ask further questions, if my answer does not solve your problem yet.
I've found the problem, it has to do with that I didn't select the right database. So stupid, Thanks for your time
Of course, if you select the wrong database then the data may already exist. You are welcome. I will update my answer to also contain that hint. Could you then please mark it as the correct answer so that future users with the same problem directly know where to look?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.