django ModelFormWithFileField, ModelWithFileField - python

I am trying to import ModelWithFileField without sucsess.
here https://docs.djangoproject.com/en/1.10/topics/http/file-uploads/ it says to import in the following manner:
from .models import ModelWithFileField
this does not work for me, I get:
ImportError: cannot import name ModelWithFileField
I have upgraded to the latest version of django
I get the same error when trying to do this:
from .forms import ModelFormWithFileField
Anyone out there know how to get these imports working?

No. That is an example of a model that contains a FileField. You need to use your own model.

Related

Unable to call a class using Django

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()

Importing Model Class into another app - Django

I am trying to import a model class from another app. My structure looks like the following:
mysite/
-- main/
models.py
-- webshop/
models.py
I'd like to import a model class from my webshop app into the main/models.py. I run the following in my main/models.py file:
from django.db import models
from ..webshop.models import Item
# Create your models here.
class Test(models.Model):
item = models.ForeignKey(Item, on_delete=models.CASCADE)
In my text editor everything seems fine. It finds the appropriate app and finds the model class Item which I need to import there.
When I run makemigrations I am getting the following error:
ValueError: attempted relative import beyond top-level package
I've read some other questions on SO on how to make this work but can't figure it out. Tried:
mysite.webshop.models import Item
aswell. But then I get a: ModuleNotFoundError: No module named 'mysite.webshop'.
Does anyone have suggestions?
From the error ValueError: attempted relative import beyond top-level package, I'm going to assume in your text editor, your current working directory is mysite/. And so using ..webshop.models you're trying to go up two directories above mysite/ which is why you're getting the error.
What you need to do is:
from django.db import models
from webshop.models import Item

Python, Flask, SQLAlchemy: cannot import from models

I've got a weird problem.
I am building a Flask app with SQLAlchemy. I have a file with models, namely, models.py. And I have a User model there.
If I open my "views.py" and insert a string
import models
and then use the User model like
u=models.User.query.filter_by(name='John',password='Doe').first()
everything works fine.
But if instead of "import models" i put
from models import User
Python crashes and says:
ImportError: cannot import name User
how can this be possible?
you most likely have a circular import; your, lets say 'app' module:
# app.py
import models
...
def doSomething():
models.User....
but your models module also imports app
import app
class User:
...
since models imports app, and app imports models, python has not finished importing models at the point app tries to import models.User; the User class has not been defined (yet). Either break the cyclic import (make sure models doesn't import anything that also imports models), or you'll just have to make do with models.User instead of the shorter User in app.
Instead of
from models import User
use
from models import *
In this case, you are importing the models into views.py therefore if you need a class from models, import it from views.py and the circular import problem will be resolved.

Django Import Export: Documentation cannot be understood

I wish to use django import export.
https://pypi.python.org/pypi/django-import-export
While reading the documentation and after installing and configuring the package, I reached the "Creating Import-Export Resource' paragraph:
https://django-import-export.readthedocs.org/en/latest/getting_started.html#creating-import-export-resource
In which directory/file should I insert and save those lines of python that will create the resource?
You can create this file anywhere in your project and then just import it where it's necessary.
So, following the docs, when your app is called core:
#myproject/core/myresources.py
from import_export import resources
from core.models import Book
class BookResource(resources.ModelResource):
class Meta:
model = Book
And then anywhere in your project's code:
from core.myresources import BookResource
dataset = BookResource().export()

Issues with facebook profile model

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

Categories