Connecting Models from different Apps - Django - python

Given that I have 4 models:
Project
User (from django.contrib.auth.models)
Sentence
Translation
My django project directories are structured as such:
\myprojname
\project
models.py
\data
models.py
The User model is independent of the Projects and Sentences models but it is linked to the Projects model through the ForeignKey:
myprojname/projects/models.py looks like this:
from django.db import models
from django.contrib.auth.models import User
class Project(models.Model):
project_id = models.AutoField(primary_key=True)
created_on = models.DateTimeField(auto_now_add=True)
updated_on = models.DateTimeField(auto_now=True)
name = models.CharField(max_length=140)
# Admin user for the particular *Project* object.
# Have the rights to write to the *Sentence* model.
owner = models.ForeignKey(User)
# Worker users that only have write access to the *Translation model.
workers = models.ForeignKey(User, blank=True)
myprojname/data/models.py looks like this:
from django.db import models
from django.contrib.auth.models import User
class Sentence(models.Model):
text = models.TextField()
translations = models.ManyToManyField('Translation', blank=True)
class Translation(models.Model):
text = models.TextField()
translator = models.ForeignKey(User)
Now the Project model isn't connected to the Sentence model.
I've tried connecting it with relative imports, i.e. in myprojname/projects/models.py, I did:
from django.db import models
from django.contrib.auth.models import User
from ..data import Sentence
class Project(models.Model):
# ...
sentences = models.ForeignKey(Sentence)
but it returned a ValueError in Python2.7:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/usr/local/lib/python2.7/dist-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/alvas/data-project/myprojname/project/models.py", line 5, in <module>
from ..data import Sentence
ValueError: Attempted relative import beyond toplevel package
And in Python3, the same error:
Traceback (most recent call last):
File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/home/alvas/.virtualenvs/payer/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 662, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/alvas/data-project/myprojname/project/models.py", line 5, in <module>
from ..data import Sentence
ValueError: attempted relative import beyond top-level package
And when I tried to do a non-relative import in Python3:
from django.db import models
from django.contrib.auth.models import User
from myprojname.data import Sentence
class Project(models.Model):
# ...
sentences = models.ForeignKey(Sentence)
I get:
Traceback (most recent call last):
File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/home/alvas/.virtualenvs/payer/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 662, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/alvas/data-project/myprojname/project/models.py", line 5, in <module>
from myprojname.data import Sentence
ImportError: No module named 'myprojname.data'
How do I connect the models from different apps?
Edited
When I used from data import Sentence in myprojname.project.models.py, it throws an ImportError:
Traceback (most recent call last):
File "/home/alvas/.virtualenvs/statnlp/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/alvas/.virtualenvs/statnlp/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/home/alvas/.virtualenvs/statnlp/lib/python3.5/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/home/alvas/.virtualenvs/statnlp/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/alvas/.virtualenvs/statnlp/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/alvas/.virtualenvs/statnlp/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/alvas/.virtualenvs/statnlp/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/home/alvas/.virtualenvs/statnlp/lib/python3.5/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/home/alvas/.virtualenvs/statnlp/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 662, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/alvas/data-project/myprojname/project/models.py", line 5, in <module>
from data import Sentence
ImportError: cannot import name 'Sentence'

Your import should be from data.models import Sentence instead of from myprojname.data import Sentence.

Related

Invalid syntax django

I'm trying to run a django code an imx6 yocto build that i made. The basic example went fine and smooth. So i decided to run my own django production from a project im working and i get the following:
root#imx6ulevk:/home/mdwb-main# python manage.py runserver 147.106.17.9:8000
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "/usr/lib/python3.5/site-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "/usr/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/usr/lib/python3.5/site-packages/django/utils/autoreload.py", line 77, in raise_last_exception
raise _exception[1]
File "/usr/lib/python3.5/site-packages/django/core/management/__init__.py", line 337, in execute
autoreload.check_errors(django.setup)()
File "/usr/lib/python3.5/site-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "/usr/lib/python3.5/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/lib/python3.5/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/usr/lib/python3.5/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 985, in _gcd_import
File "<frozen importlib._bootstrap>", line 968, in _find_and_load
File "<frozen importlib._bootstrap>", line 957, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 693, in exec_module
File "<frozen importlib._bootstrap_external>", line 799, in get_code
File "<frozen importlib._bootstrap_external>", line 759, in source_to_code
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/mdwb-main/api_cloud/models.py", line 15
return f'{self.cloud_interval} foi cadastrado com sucesso!'
^
SyntaxError: invalid syntax
The code in models.py is the following:
from django.db import models
# Create your models here.
class Initial_config(models.Model):
#complement = models.CharField(max_length=30,null=True)
cloud_interval = models.IntegerField()
device_interval = models.IntegerField()
class Meta:
verbose_name = 'config'
verbose_name_plural = 'configs'
def __str__(self):
return f'{self.cloud_interval} foi cadastrado com sucesso!'
I tried to change the ' to " remove the {self.cloud_interval}, but none of them was sucessful.
Why this happens? How to fix it?
f-strings can only be used from Python 3.6 onwards.
If you want to remain with Python 3.5, you can change your code to:
def __str__(self):
return '%s foi cadastrado com sucesso!' % self.cloud_interval
That said, Python 3.5 is no longer officially supported; upgrading to 3.6 or above is highly recommended. See https://endoflife.date/python for more.

Error in importing a module in rest_framework, while creating rest API using Django framework

I created a web application using Djanjo framework, and then when I am trying to create a REST API, after configuring all the relevant files such as models.py, admin.py, serializers.py, views.py, and settings.py. It shows an error while importinng the model from rest_framework. The traceback is as follows:
*Traceback (most recent call last):
File "C:\Users\dmandal\AppData\Local\Continuum\anaconda3\lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "C:\Users\dmandal\AppData\Local\Continuum\anaconda3\lib\threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\dmandal\AppData\Local\Continuum\anaconda3\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "C:\Users\dmandal\AppData\Local\Continuum\anaconda3\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "C:\Users\dmandal\AppData\Local\Continuum\anaconda3\lib\site-packages\django\core\management\base.py", line 390, in check
include_deployment_checks=include_deployment_checks,
File "C:\Users\dmandal\AppData\Local\Continuum\anaconda3\lib\site-packages\django\core\management\base.py", line 377, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Users\dmandal\AppData\Local\Continuum\anaconda3\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\dmandal\AppData\Local\Continuum\anaconda3\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\dmandal\AppData\Local\Continuum\anaconda3\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\dmandal\AppData\Local\Continuum\anaconda3\lib\site-packages\django\urls\resolvers.py", line 399, in check
for pattern in self.url_patterns:
File "C:\Users\dmandal\AppData\Local\Continuum\anaconda3\lib\site-packages\django\utils\functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\dmandal\AppData\Local\Continuum\anaconda3\lib\site-packages\django\urls\resolvers.py", line 584, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\dmandal\AppData\Local\Continuum\anaconda3\lib\site-packages\django\utils\functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\dmandal\AppData\Local\Continuum\anaconda3\lib\site-packages\django\urls\resolvers.py", line 577, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\dmandal\AppData\Local\Continuum\anaconda3\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\myproject1\myproject1\urls.py", line 19, in <module>
from webapp import views
File "C:\myproject1\webapp\views.py", line 9, in <module>
from . serializers import employeesSerializer
File "C:\myproject1\webapp\serializers.py", line 2, in <module>
from rest_framework import employees
ImportError: cannot import name 'employees' from 'rest_framework' (C:\Users\dmandal\AppData\Local\Continuum\anaconda3\lib\site-packages\rest_framework\__init__.py)*
The serializer.py is as follows:
from rest_framework import serializers
from rest_framework import employees
class employeesSerializer(serializers.ModelSerializer):
class Meta:
model = employees
# fields = ('firstname','lastname')
fields = '__all__'
It's name conflict. Please change the name of your app to something else other then rest_framework.

'tuple' object is not callable , machine learning API using Django API

I am trying build a machine learning API using DJANGO rest framework API. After creating a project and configuring model.py of created project 'MyAPI' ,for required approvals. And used the following commend to run the server, got following error.
how to fix them ?, Please guide me.
````C:\Users\Padmini\DjangoAPI>python manage.py runserver```
error
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\Padmini\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 926, in _bootstrap_inner
self.run()
File "C:\Users\Padmini\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Padmini\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\Padmini\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\commands\runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "C:\Users\Padmini\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\autoreload.py", line 76, in raise_last_exception
raise _exception[1]
File "C:\Users\Padmini\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 357, in execute
autoreload.check_errors(django.setup)()
File "C:\Users\Padmini\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\Padmini\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\Padmini\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\Users\Padmini\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\apps\config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\Padmini\AppData\Local\Programs\Python\Python37-32\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\Padmini\DjangoAPI\MyAPI\models.py", line 4, in <module>
class approvals(models.Model):
File "C:\Users\Padmini\DjangoAPI\MyAPI\models.py", line 7, in approvals
('Female','Female')
TypeError: 'tuple' object is not callable````
my model.py as following code
from django.db import models
# Create your models here.
class approvals(models.Model):
GENDER_CHOICES= (
('Male', 'Male')
('Female','Female')
)
MARRIED_CHOICES=(
('Yes','Yes'),
('No','No')
)
GRADUATED_CHOICES= (
('Graduate','Graduated'),
('Not_Graduate','Not_Graduate')
)
SELFEMPLOYED_CHOICES=(
('Yes','Yes'),
('No','No')
)
PROPERTY_CHOICES=(
('Rural','Rural'),
('Semiurban','Semiurban'),
('Urban','Urban')
)
firstname=models.CharField(max_length=15)
lastname=models.CharField(max_length=15)
dependants=models.IntegerField(default=0)
applicantincome=models.IntegerField(default=0)
coapplicatincome=models.IntegerField(default=0)
loanamt=models.IntegerField(default=0)
loanterm=models.IntegerField(default=0)
credithistory=models.IntegerField(default=0)
gender=models.CharField(max_length=15, choices=GENDER_CHOICES)
married=models.CharField(max_length=15, choices=MARRIED_CHOICES)
graduatededucation=models.CharField(max_length=15, choices=GRADUATED_CHOICES)
selfemployed=models.CharField(max_length=15, choices=SELFEMPLOYED_CHOICES)
area=models.CharField(max_length=15, choices=PROPERTY_CHOICES)
def __str__(self):
return '{}, {}'.format(self.lastname, self.firstname)`````

Proxy model error: Local field in class clashes with field of the same name from base class

class EmailUser(User):
class Meta:
proxy = True
email = models.EmailField(_('email address'), unique=True)
In the above, I'm trying to implement a proxy model, but I'm getting this error message:
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x10e04ed08>
Traceback (most recent call last):
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/utils/autoreload.py", line 228, in wrapper
fn(*args, **kwargs)
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
autoreload.raise_last_exception()
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/utils/autoreload.py", line 251, in raise_last_exception
six.reraise(*_exception)
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/utils/autoreload.py", line 228, in wrapper
fn(*args, **kwargs)
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models()
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/workout/workoutcal/models.py", line 28, in <module>
class EmailUser(User):
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/db/models/base.py", line 231, in __new__
base.__name__,
django.core.exceptions.FieldError: Local field 'username' in class 'EmailUser' clashes with field of the same name from base class 'User'.
I wonder why I get this error, and what to do about it. I'm using Django version 1.11.
You can't do what you're trying to do with a proxy model. Luckily, Django makes it easy to extend the built-in User model: create a normal concrete model that inherits from AbstractUser, and set the AUTH_USER_MODEL setting to the name of your model.

ArrayField missing 1 required positional argument

I have imported the following header file
from django.contrib.postgres.fields import ArrayField
Used the following in the model
question_array = ArrayField(models.IntegerField, blank=True,)
i get the following error
Traceback (most recent call last)
File "C:\Program Files (x86)\Python\Python35-32\lib\site-packages\django-1.10.4-py3.5.egg\django\__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Program Files (x86)\Python\Python35-32\lib\site-packages\django-1.10.4-py3.5.egg\django\apps\registry.py", line 108, in populate
app_config.import_models(all_models)
File "C:\Program Files (x86)\Python\Python35-32\lib\site-packages\django-1.10.4-py3.5.egg\django\apps\config.py", line 199, in import_models
self.models_module = import_module(models_module_name)
File "C:\Program Files (x86)\Python\Python35-32\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "C:\Users\DELL\Desktop\cstrom\comp\models.py", line 24, in <module>
class User(models.Model):
File "C:\Program Files (x86)\Python\Python35-32\lib\site-packages\django-1.10.4-py3.5.egg\django\db\models\base.py", line 157, in __new__
new_class.add_to_class(obj_name, obj)
File "C:\Program Files (x86)\Python\Python35-32\lib\site-packages\django-1.10.4-py3.5.egg\django\db\models\base.py", line 316, in add_to_class
value.contribute_to_class(cls, name)
File "C:\Program Files (x86)\Python\Python35-32\lib\site-packages\django-1.10.4-py3.5.egg\django\db\models\fields\__init__.py", line 689, in contribute_to_class
self.set_attributes_from_name(name)
File "C:\Program Files (x86)\Python\Python35-32\lib\site-packages\django-1.10.4-py3.5.egg\django\contrib\postgres\fields\array.py", line 75, in set_attributes_from_name
self.base_field.set_attributes_from_name(name)
TypeError: set_attributes_from_name() missing 1 required positional argument: 'name'
As per the docs
You still need to fully define the field inside the array field
question_array = ArrayField(models.IntegerField(null=True, blank=True), blank=True,)
models.IntegerField is a function not a property, so you will be required to call it like a function. () at the end
question_array = ArrayField(models.IntegerField(blank=True), blank=True,)
Read more about it here.

Categories

Resources