I'm adding a Profile to my Django app as a way to store more User information. Following https://simpleisbetterthancomplex.com/tutorial/2016/07/22/how-to-extend-django-user-model.html#onetoone
However I'm getting an error when testing it. I'm using the default User model from django.contrib.auth.
models.py
from django.db import models
from django.contrib.auth import get_user_model
from django.db.models.signals import post_save
from django.dispatch import receiver
class Profile(models.Model):
user = models.OneToOneField(get_user_model(), on_delete=models.CASCADE)
#receiver(post_save, sender=get_user_model())
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
#receiver(post_save, sender=get_user_model())
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
tests/test_model.py
from django.test import TestCase
from app.models import Profile
class ProfileModelTests(TestCase):
#classmethod
def setUpTestData(cls):
Profile.objects.create()
def test_can_create_profile(self):
pass
error
/Users/user/.local/share/virtualenvs/project-api-HgI9cQzm/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
""")
..........E............
======================================================================
ERROR: setUpClass (project-api.polls.tests.test_models.ProfileModelTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/user/.local/share/virtualenvs/project-api-HgI9cQzm/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
psycopg2.IntegrityError: null value in column "user_id" violates not-null constraint
DETAIL: Failing row contains (158, null).
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/user/.local/share/virtualenvs/project-api-HgI9cQzm/lib/python3.6/site-packages/django/test/testcases.py", line 1002, in setUpClass
cls.setUpTestData()
File "/Users/user/programming/gatsby/project-api/polls/tests/test_models.py", line 86, in setUpTestData
Profile.objects.create()
File "/Users/user/.local/share/virtualenvs/project-api-HgI9cQzm/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/user/.local/share/virtualenvs/project-api-HgI9cQzm/lib/python3.6/site-packages/django/db/models/query.py", line 417, in create
obj.save(force_insert=True, using=self.db)
File "/Users/user/.local/share/virtualenvs/project-api-HgI9cQzm/lib/python3.6/site-packages/django/db/models/base.py", line 729, in save
force_update=force_update, update_fields=update_fields)
File "/Users/user/.local/share/virtualenvs/project-api-HgI9cQzm/lib/python3.6/site-packages/django/db/models/base.py", line 759, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/Users/user/.local/share/virtualenvs/project-api-HgI9cQzm/lib/python3.6/site-packages/django/db/models/base.py", line 842, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/Users/user/.local/share/virtualenvs/project-api-HgI9cQzm/lib/python3.6/site-packages/django/db/models/base.py", line 880, in _do_insert
using=using, raw=raw)
File "/Users/user/.local/share/virtualenvs/project-api-HgI9cQzm/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/user/.local/share/virtualenvs/project-api-HgI9cQzm/lib/python3.6/site-packages/django/db/models/query.py", line 1125, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/Users/user/.local/share/virtualenvs/project-api-HgI9cQzm/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1283, in execute_sql
cursor.execute(sql, params)
File "/Users/user/.local/share/virtualenvs/project-api-HgI9cQzm/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/Users/user/.local/share/virtualenvs/project-api-HgI9cQzm/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/Users/user/.local/share/virtualenvs/project-api-HgI9cQzm/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/Users/user/.local/share/virtualenvs/project-api-HgI9cQzm/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/user/.local/share/virtualenvs/project-api-HgI9cQzm/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: null value in column "user_id" violates not-null constraint
DETAIL: Failing row contains (158, null).
----------------------------------------------------------------------
Ran 22 tests in 2.198s
FAILED (errors=1)
Using existing test database for alias 'default'...
System check identified no issues (0 silenced).
Preserving test database for alias 'default'...
#receiver(post_save, sender=get_user_model())
With this code, you are listening to the save method of the User model to create a related Profile entry just in time, if there isn't one yet. This way, you can just go along creating new Users without worrying at creating and linking Profiles manually everytime.
Note that the reverse isn't covered: if you want to create a Profile entry directly, you must pass an existing User object to it, or else you will get an IntegrityError.
Solution: preferrably, stick to one main way to create user + profile pairs; in your case, just create a User and let the signal call the creation of the profile automatically.
class ProfileModelTests(TestCase):
#classmethod
def setUpTestData(cls):
get_user_model().objects.create()
def test_profile(self):
profile = get_user_model().objects.last().profile
The idea is Profile.objects.create(user=instance) if the user doesn't exist yet an IntegrityError exception will be thrown since no user_id to be found. The signals will listen when the user will be created not the reverse.
In your testcase Profile.objects.create() will try to create a One-To-One relation with a user that doesn't exist.
Related
How can I generate a UUIDField that works for SQLite?
I want to use SQLite instead of Postgres for my tests so they run faster.
# settings.py
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
# ...
}
}
# Tests use sqlite instead of postgres
import sys
if (
"test" in sys.argv or "test_coverage" in sys.argv
): # Covers regular testing and django-coverage
DATABASES["default"]["ENGINE"] = "django.db.backends.sqlite3"
However, I don't seem to be able to create a UUID that fits Django's UUIDField for SQLite:
A field for storing universally unique identifiers. Uses Python’s UUID class. When used on PostgreSQL, this stores in a uuid datatype, otherwise in a char(32).
The following doesn't work even though the uuid value is 32 chars:
# models.py
class Item(models.Model):
uuid = models.UUIDField()
# tests.py
uuid = str(uuid.uuid4()).replace("-", "")
Item.objects.create(uuid=uuid)
I get this error: django.db.utils.InterfaceError: Error binding parameter 4 - probably unsupported type.
Edit:
Here is the full error:
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 423, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.InterfaceError: Error binding parameter 4 - probably unsupported type.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/app/library/tests/tests_views_auth.py", line 423, in test_post_list_items_works
c1 = Chapter.objects.create(title="B1C1", body="B1C1", parent=b1)
File "/24reads/library/models.py", line 117, in create
return super().create(**kwargs)
File "/usr/local/lib/python3.9/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/django/db/models/query.py", line 453, in create
obj.save(force_insert=True, using=self.db)
File "/usr/local/lib/python3.9/site-packages/django_lifecycle/mixins.py", line 134, in save
save(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/mptt/models.py", line 1091, in save
self.insert_at(
File "/usr/local/lib/python3.9/site-packages/mptt/models.py", line 771, in insert_at
self._tree_manager.insert_node(
File "/usr/local/lib/python3.9/site-packages/mptt/managers.py", line 42, in wrapped
return getattr(self._base_manager, method.__name__)(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/mptt/managers.py", line 43, in wrapped
return method(self, *args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/mptt/managers.py", line 537, in insert_node
self._create_space(2, space_target, tree_id)
File "/usr/local/lib/python3.9/site-packages/mptt/managers.py", line 816, in _create_space
self._manage_space(size, target, tree_id)
File "/usr/local/lib/python3.9/site-packages/mptt/managers.py", line 1052, in _manage_space
cursor.execute(
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 423, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.InterfaceError: Error binding parameter 4 - probably unsupported type.
----------------------------------------------------------------------
My specific use case sees me override the base MPTTModel and TreeManager to use a UUID instead of a continuguous integer to avoid concurrency issues as outlined here: https://github.com/django-mptt/django-mptt/issues/555#issuecomment-331315715
class AsyncSafeTreeManager(TreeManager):
def _get_next_tree_id(self):
if (
"test" in sys.argv or "test_coverage" in sys.argv
): # Covers regular testing and django-coverage
return uuid.uuid4()
return generate_ulid_as_uuid()
class AsyncSafeMPTTModel(MPTTModel):
objects = AsyncSafeTreeManager()
tree_id = models.UUIDField()
class Meta:
abstract = True
This works fine when I'm using PostGRE but does not work with SQLite.
Regardless what the underlying type is, Django will transform a UUID to the correct format, and insert it in the database. For SQLite that thus means that the UUIDField will tranform it to a string.
You thus can create an item with:
import uuid
Item.objects.create(
uuid=uuid.uuid4()
)
I'm trying to use Django's factory-bot module to create factories for my models. I'm also using pytest. I have created these factories ...
import factory
from maps.models import CoopType, Coop
from address.models import AddressField
from phonenumber_field.modelfields import PhoneNumberField
from address.models import State, Country, Locality
class CountryFactory(factory.DjangoModelFactory):
"""
Define Country Factory
"""
class Meta:
model = Country
name = "Narnia"
code = "NN"
class StateFactory(factory.DjangoModelFactory):
"""
Define State Factory
"""
class Meta:
model = State
name = "Narnia"
code = "NN"
country = CountryFactory()
class LocalityFactory(factory.DjangoModelFactory):
"""
Define Locality Factory
"""
class Meta:
model = Locality
name = "Narnia"
postal_code = "60605"
state = StateFactory()
class AddressFactory(factory.DjangoModelFactory):
"""
Define Address Factory
"""
class Meta:
model = Address
street_number = "123"
route = "Rd"
raw = "123 Fake Rd"
formatted = "123 Fake Rd."
latitude = 87.1234
longitude = -100.12342
locality = LocalityFactory()
class CoopTypeFactory(factory.DjangoModelFactory):
"""
Define Coop Type Factory
"""
class Meta:
model = CoopType
I have a very simple test file thus far. It only has one test ...
import pytest
from django.test import TestCase
from .factories import CoopTypeFactory, CoopFactory
class ModelTests(TestCase):
#classmethod
def setUpTestData(cls):
print("setUpTestData: Run once to set up non-modified data for all class methods.")
#management.call_command('loaddata', 'test_data.yaml', verbosity=0)
pass
def setUp(self):
print("setUp: Run once for every test method to setup clean data.")
#management.call_command('flush', verbosity=0, interactive=False)
pass
#pytest.mark.django_db
def test_coop_type_create(self):
""" Test customer model """ # create customer model instance
coop_type = CoopTypeFactory(name="Test Coop Type Name")
assert coop_type.name == "Test Coop Type Name"
But when I run my test, it dies complaining about a duplicate key for a factory that I'm not even creating.
davea$ python manage.py test --settings=maps.test_settings
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
E
======================================================================
ERROR: tests.test_models (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: tests.test_models
Traceback (most recent call last):
File "/Users/davea/Documents/workspace/chicommons/maps/web
...
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py", line 434, in _find_test_path
module = self._get_module_from_name(name)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/loader.py", line 375, in _get_module_from_name
__import__(name)
File "/Users/davea/Documents/workspace/chicommons/maps/web/tests/test_models.py", line 3, in <module>
from .factories import CoopTypeFactory, CoopFactory
File "/Users/davea/Documents/workspace/chicommons/maps/web/tests/factories.py", line 19, in <module>
class StateFactory(factory.DjangoModelFactory):
File "/Users/davea/Documents/workspace/chicommons/maps/web/tests/factories.py", line 28, in StateFactory
country = CountryFactory()
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/factory/base.py", line 46, in __call__
return cls.create(**kwargs)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/factory/base.py", line 564, in create
return cls._generate(enums.CREATE_STRATEGY, kwargs)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/factory/django.py", line 141, in _generate
return super(DjangoModelFactory, cls)._generate(strategy, params)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/factory/base.py", line 501, in _generate
return step.build()
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/factory/builder.py", line 279, in build
kwargs=kwargs,
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/factory/base.py", line 315, in instantiate
return self.factory._create(model, *args, **kwargs)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/factory/django.py", line 185, in _create
return manager.create(*args, **kwargs)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/models/query.py", line 417, in create
obj.save(force_insert=True, using=self.db)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/models/base.py", line 729, in save
force_update=force_update, update_fields=update_fields)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/models/base.py", line 759, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/models/base.py", line 842, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/models/base.py", line 880, in _do_insert
using=using, raw=raw)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/models/query.py", line 1125, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1280, in execute_sql
cursor.execute(sql, params)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 71, in execute
return self.cursor.execute(query, args)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/MySQLdb/cursors.py", line 209, in execute
res = self._query(query)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/MySQLdb/cursors.py", line 315, in _query
db.query(q)
File "/Users/davea/Documents/workspace/chicommons/maps/web/venv/lib/python3.7/site-packages/MySQLdb/connections.py", line 239, in query
_mysql.connection.query(self, query)
django.db.utils.IntegrityError: (1062, "Duplicate entry 'Narnia' for key 'name'")
----------------------------------------------------------------------
Ran 1 test in 0.000s
What am I doing wrong? Why is Django trying to create a factory for something I'm not creating a factory for?
You should use factory.SubFactory for defining foreign key relationships
class LocalityFactory(factory.DjangoModelFactory):
...
state = factory.SubFactory(StateFactory)
Calling the factories in your classes is creating instances
This is my Models:
from django.db import models
from django.contrib.auth.models import User
class Bookmark(models.Model):
id = models.PositiveIntegerField(primary_key=True)
name = models.CharField(max_length=50)
url = models.URLField()
isPublic = models.BooleanField(default=True)
dateTimePosted = models.DateTimeField(auto_now=True,verbose_name="posted at")
user = models.ForeignKey(User, on_delete=models.CASCADE)
This is my 0001_initials.py:
from __future__ import unicode_literals
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='Bookmark',
fields=[
('id', models.PositiveIntegerField(primary_key=True, serialize=False)),
('name', models.CharField(max_length=50)),
('url', models.URLField()),
('isPublic', models.BooleanField(default=True)),
('dateTimePosted', models.DateTimeField(auto_now=True, verbose_name='posted at')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
When I am inserting values using:
python manage.py shell:
>>from django.contrib.auth.models import User
>>user = User.objects.create_user('test', 'test#abc.com', 'test#123')
>>from bookmark.models import Bookmark
>>bookmark = Bookmark.objects.create(name='First Bookmark', url='example.com', user=user)
this error occurs:
Traceback (most recent call last):
File "", line 1, in
File "C:\Python34\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Python34\lib\site-packages\django\db\models\query.py", line 394, in create
obj.save(force_insert=True, using=self.db)
File "C:\Python34\lib\site-packages\django\db\models\base.py", line 806, in save
force_update=force_update, update_fields=update_fields)
File "C:\Python34\lib\site-packages\django\db\models\base.py", line 836, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "C:\Python34\lib\site-packages\django\db\models\base.py", line 922, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "C:\Python34\lib\site-packages\django\db\models\base.py", line 961, in _do_insert
using=using, raw=raw)
File "C:\Python34\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Python34\lib\site-packages\django\db\models\query.py", line 1063, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Python34\lib\site-packages\django\db\models\sql\compiler.py", line 1099, in execute_sql
cursor.execute(sql, params)
File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 80, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "C:\Python34\lib\site-packages\django\db\utils.py", line 94, in exit
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Python34\lib\site-packages\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "C:\Python34\lib\site-packages\django\db\backends\sqlite3\base.py", line 328, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: bookmark_bookmark.id
how to tackle this error
I have deleted database (db.sqlite3)and pycache folder unapplied migrations and then created again using:
python manage.py makemigrations
but still error occurs
You are not passing the primary key value (id) when you are creating your BookMark object.
Change your create as follows,
bookmark = Bookmark.objects.create(name='First Bookmark', url='example.com', user=user, id=<some_int_which_hasnt_been_used>) # Use an id that you know you hasn't been used.
Now the reason you have to do this is because you have defined your own Primary Key, so Django will not automatically create it for you.
When you do not specify a primary key, Django uses an "id" field which is of type "models.AutoField", and automatically increments the value (without you needing to specify the value for id).
In case you are interested in knowing how it works internally you can read up on Sequences in databases (what Django does internally is create a sequence in your database for the table, and your database has a default value for your id column)
I'm not sure why you have overridden the id field that Django provides automatically (because your id is PositiveIntegerField, and AutoField would generate only positive values anyway), so if there is no specific reason for you defining the PK, I suggest you go with what Django creates for you.
I want to extend my 'User' model with the 'Profile' model. To facilitate this I've created the following model. I wanted to have the linked 'Profile' model be automatically created with each new 'User' model. Based on some comments on stackoverflow / research on the internet (simpleisbetterthancomplex) I came with the following solution:
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
# Create your models here.
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
#Pushup related stats
total_pushups = models.IntegerField(default=0)
best_consecutive = models.IntegerField(default=0)
week_streak = models.IntegerField(default=0)
save = models.IntegerField(default=000)
#receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
#receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
However, every time I run this (whether through unit tests or 'create superuser' - I have no vies yet, I'm practicing TDD) I get the following error:
TypeError: 'int' object is not callable
Does anyone here know what I am doing wrong?
Edit: I've included a traceback of the error message that is shown after the 'create superuser' command
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 63, in execute
return super(Command, self).execute(*args, **options)
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 183, in handle
self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/contrib/auth/models.py", line 170, in create_superuser
return self._create_user(username, email, password, **extra_fields)
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/contrib/auth/models.py", line 153, in _create_user
user.save(using=self._db)
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/contrib/auth/base_user.py", line 80, in save
super(AbstractBaseUser, self).save(*args, **kwargs)
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/db/models/base.py", line 807, in save
force_update=force_update, update_fields=update_fields)
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/db/models/base.py", line 847, in save_base
update_fields=update_fields, raw=raw, using=using,
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/dispatch/dispatcher.py", line 193, in send
for receiver in self._live_receivers(sender)
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/dispatch/dispatcher.py", line 193, in <listcomp>
for receiver in self._live_receivers(sender)
File "/home/jasper/PycharmProjects/PushUpTuneUp/user_profiles/models.py", line 20, in create_user_profile
Profile.objects.create(user=instance)
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/jasper/PycharmProjects/PushUpTuneUp/venv/lib/python3.5/site-packages/django/db/models/query.py", line 394, in create
obj.save(force_insert=True, using=self.db)
TypeError: 'int' object is not callable
You've defined a field called save, which is hiding the actual save method. Rename it to something else.
(And not related to your problem, but you certainly shouldn't have two receivers here. I can't see why you need the second one at all, but even if you did need that logic you should just put it in with the first one.)
I am new to Django, want to create custom user models as follows:
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.db import models
class UniservedTeam(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
Role = ArrayField(models.CharField(max_length=1000), blank=True,null=True)
ContactNumber = models.CharField(max_length=100)
In shell I am trying to create an user :
>>> user = User.objects.create(email='piyush#uniserved.com',password='Piyush#123',username='Piyush#24',last_name='Wanare',first_name='Piyush',is_active=True)
>>> user
<User: Piyush#24>
>>> user.id
1
>>> team = UniservedTeam.objects.create(id=user.id,Role=['Vendor Co-ordinator'],ContactNumber='862339798167')
Getting Error as follows:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/piyush/.environments/awsd/local/lib/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/piyush/.environments/awsd/local/lib/python2.7/site-packages/django/db/models/query.py", line 401, in create
obj.save(force_insert=True, using=self.db)
File "/home/piyush/.environments/awsd/local/lib/python2.7/site-packages/django/db/models/base.py", line 700, in save
force_update=force_update, update_fields=update_fields)
File "/home/piyush/.environments/awsd/local/lib/python2.7/site-packages/django/db/models/base.py", line 728, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/piyush/.environments/awsd/local/lib/python2.7/site-packages/django/db/models/base.py", line 812, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/piyush/.environments/awsd/local/lib/python2.7/site-packages/django/db/models/base.py", line 851, in _do_insert
using=using, raw=raw)
File "/home/piyush/.environments/awsd/local/lib/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/piyush/.environments/awsd/local/lib/python2.7/site-packages/django/db/models/query.py", line 1039, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/piyush/.environments/awsd/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1060, in execute_sql
cursor.execute(sql, params)
File "/home/piyush/.environments/awsd/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/piyush/.environments/awsd/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/piyush/.environments/awsd/local/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/piyush/.environments/awsd/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
IntegrityError: null value in column "user_id" violates not-null constraint
I think what you want instead after you make your user is this
team = UniservedTeam.objects.create(user=user,Role=['Vendor Co-ordinator'],ContactNumber='862339798167')
Try this,
team = UniservedTeam.objects.create(user__id=user.id, Role=['Vendor Co-ordinator'],ContactNumber='862339798167')