ImportError cannot import name user

0 votes

In Django I created a new model:

from django.db import models
from django.contrib.auth import user

class Workers(models.Model):
    user = models.OneToOneField(User, primary_key=True)
        work_group = models.CharField(max_length=20)
        card_num = models.IntegerField()
    def __unicode__(self):
            return self.user

But it doesn't work: ImportError: cannot import name user

How to fix it?

So, I want to create a new table "workers" in db, which has a OneToOne relationship with table "auth_user". Thanks.

Aug 17, 2020 in Python by kartik
• 37,520 points
8,029 views

1 answer to this question.

0 votes

Hello @kartik,

You missed the models - and user is capitalized in:

from django.contrib.auth.models import User

If you use a custom user model you should use:

from django.contrib.auth import get_user_model
User = get_user_model()

Hope it helps!!
Thank You!

answered Aug 17, 2020 by Niroj
• 82,840 points

Related Questions In Python

0 votes
1 answer

How to resolve ImportError: cannot import name 'add' in python ?

There's no module called "add" in random. ...READ MORE

answered Feb 13, 2019 in Python by Mark
4,713 views
0 votes
1 answer

ImportError: cannot import name 'main' in pip

Hi@akhtar, To avoid this error, you should upgrade ...READ MORE

answered Apr 15, 2020 in Python by MD
• 95,460 points
4,984 views
0 votes
1 answer

ImportError: cannot import name 'PILLOW_VERSION' from 'PIL' (unknown location)

Hi@akhtar, I think there is a problem in ...READ MORE

answered Apr 22, 2020 in Python by MD
• 95,460 points
7,560 views
+1 vote
1 answer

ImportError: cannot import name 'appdirs'

Hi@akhtar, I think you missed some module to ...READ MORE

answered May 5, 2020 in Python by MD
• 95,460 points
3,997 views
0 votes
1 answer

How to temporarily disable a foreign key constraint in MySQL?

Hello @kartik, To turn off foreign key constraint ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,840 points
2,508 views
0 votes
1 answer

How do I use Django templates without the rest of Django?

Hello @kartik, Let's say you have this important ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,840 points
1,586 views
0 votes
1 answer

ImportError: cannot import name 'render_to_response' from 'django.shortcuts'

Hello @kartik, The render_to_response shortcut was deprecated in Django 2.0, and ...READ MORE

answered Jul 2, 2020 in Python by Niroj
• 82,840 points
8,635 views
0 votes
1 answer

ImportError: cannot import name find_spec” when I start a new Django project?

Hello @kartik, find_spec isn't available in Python 3.2.3; it ...READ MORE

answered Aug 5, 2020 in Python by Niroj
• 82,840 points
2,336 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP