I am using facebook_connect with my app.
I want to import user profile model to my app.....But it gives me an error of
from facebook_connect.models import FacebookProfileModel
ImportError: cannot import name FacebookProfileModel
my model.py is as:-
from facebook_connect.models import FacebookProfileModel
class MyCustomProfile(FacebookProfileModel):
user = models.OneToOneField('auth.User')
I am using this functionality first time and I am using this doc for reference. so any body can help me where I am wrong or I have to add something in my model.
Thanks
I'm pretty sure your import statement is incorrect, and should be django_facebook not facebook_connect.
From the docs:
from django_facebook.models import FacebookProfileModel
class MyCustomProfile(FacebookProfileModel):
user = models.OneToOneField('auth.User')
....
Related
I created a models within my page but when I attempted to run the page I received an error response
celery_beat_1 | class ClassManager(models.Manager):
celery_beat_1 | NameError: name 'models' is not defined
I searched for fixes to this error online and most of the responses said to implement the
import from django.db import models
function. However, this is something I already have configured in my models page. Any idea on how to proceed forward?
You should import models from django in models.py.
from django.db import models
class MyModel(models.Model):
pass
You can check more information in django documentation itself:
https://docs.djangoproject.com/en/3.2/topics/db/models/#quick-example
You are importing the models in wrong way format, so you have to use -
from django.db import models
rather than using
import from django.db import models
If I do that :
from myapp import models
User.objects.first()
I got that error :
NameError : name 'User' is not defined
whereas if I do that
import myapp
myapp.models.User.objects.first()
it works
I don't understand at all why I have that problem
Thank you very much for your help !
Replace:
from myapp import models
with the following:
This way, you are telling Django which model classes to import rather than leaving Django guessing what to do with it.
It prevents you from loading unnecessary models which might not be used right away and could potentially increase load time.
from myapp.models import User
In your example, your have not imported class User actually. You have imported it's module called models
You can do one of these:
from myapp import models
models.User.objects.first()
Or:
from myapp.models import User
User.objects.first()
I have 2 models, Account and AccountUser. An Account has many AccountUsers. I'm trying to create this using Factory Boy's circular imports but have been unsuccessful so far with various changes. There's not much on SO about factory boy, and not enough on Google to help me. Can anyone help me make this work without errors?
I put a pdb at the bottom of stub_account.py and do acct = AccountFactory() but I get *** NameError: name 'AccountFactory' is not defined. If I do acct = AccountFactory().build() or .create() I get the same error. If I define those variables inside the script instead of while in pdb I get the same error.
project_root/app/tests/scripts/stubs/stub_account.py
from app.models import Account
from faker import Faker
import factory
fake = Faker()
class AccountFactory(factory.Factory):
class Meta:
model = Account
billing_contact = factory.SubFactory(
"app.tests.scripts.stubs.stub_account_user.AccountUserFactory")
project_root/app/tests/scripts/stubs/stub_account_user.py
from app.models import AccountUser
from faker import Faker
import factory
fake = Faker()
class AccountUserFactory(factory.Factory):
class Meta:
model = AccountUser
_parent = factory.SubFactory(AccountFactory)
For reverse foreign key you have to use factory.RelatedFactory
I'm working with Google App Engine and I want to use my ndb model in another .py file but I couldn't import it.
Here is my main.py;
from google.appengine.ext import ndb
class User(ndb.Model):
username = ndb.StringProperty()
created_date = ndb.DateTimeProperty(auto_now=True)
follower_list = ndb.StringProperty(repeated=True)
And this is some code from my cron.py file:
from google.appengine.ext import ndb
save_user = User.query().filter(User.username == username)
But I'm getting:
ImportError: No module named User
How can I import the User class?
When you create the model you're just instantiating a class and assigning it to the variable named User. In python those variables are bound to the module they were declared in, and there are no implicit globals, so if you want to use it in another module you would need to import it:
from google.appengine.ext import ndb
import main
save_user = main.User.query().filter(main.User.username == username)
However the best practice would be to create the models in a models.py file, and import that anytime you need them.
BTW, your error hints that you're trying to import User earlier in your cron file, is that so? Either way I think you should get the idea now :)
I am trying to use validators in my form fields but am getting an error:
from django import forms
from django.db import models
from django.core.exceptions import ValidationError
class Register(forms.Form):
username = forms.CharField(max_length=100,label="Username",validators=[validate_email])
>>>> name 'validate_email' is not defined
I have tried this with a number of different validator types, only to be hit with the same message for each. I have looked over the documentation and really can't see what I am missing as to how to import the validators into the class, any advice is appreciated
You seem to be missing an import. Try adding
from django.core.validators import validate_email
to your imports