Can't save new objects in Django full-text search with PostgreSQL - django

I'm following this post on how to add full-text functionality to my Django app. I have the following model:
class CustomUser(AbstractUser):
email = models.EmailField(_('email address'), unique=True)
class Author(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL,on_delete = models.CASCADE)
name = models.CharField(max_length = 250)
alternative_names = ArrayField(models.CharField(max_length = 250),blank=True,null=True)
name_norm = models.CharField(max_length = 250,blank=True,null = True)
slug = models.CharField(max_length = 250,unique = True)
photo = models.ImageField(upload_to = 'imgs/users/%Y/%m/%d',blank = True)
search_vector = SearchVectorField(null=True)
def save(self, *args, **kwargs):
self.search_vector = (SearchVector('name', weight='A') )
super().save(*args, **kwargs)`enter code here`
And when I try to save a new author with save:
autor = Author(user = utilizador,name = author_name,alternative_names = alternative_names,name_norm = name_norm,slug = slug)
autor.save()
I get the error:
Traceback (most recent call last):
File "populate_v2.py", line 140, in <module>
autor.save()
File "/home/mwon/NewArquivoOpiniao/webapp/colunadeopiniao/accounts/models.py", line 57, in save
super().save(*args, **kwargs)
File "/home/mwon/NewArquivoOpiniao/env3.8/lib/python3.8/site-packages/django/db/models/base.py", line 748, in save
self.save_base(using=using, force_insert=force_insert,
File "/home/mwon/NewArquivoOpiniao/env3.8/lib/python3.8/site-packages/django/db/models/base.py", line 785, in save_base
updated = self._save_table(
File "/home/mwon/NewArquivoOpiniao/env3.8/lib/python3.8/site-packages/django/db/models/base.py", line 890, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "/home/mwon/NewArquivoOpiniao/env3.8/lib/python3.8/site-packages/django/db/models/base.py", line 927, in _do_insert
return manager._insert(
File "/home/mwon/NewArquivoOpiniao/env3.8/lib/python3.8/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/mwon/NewArquivoOpiniao/env3.8/lib/python3.8/site-packages/django/db/models/query.py", line 1204, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "/home/mwon/NewArquivoOpiniao/env3.8/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1393, in execute_sql
for sql, params in self.as_sql():
File "/home/mwon/NewArquivoOpiniao/env3.8/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1336, in as_sql
value_rows = [
File "/home/mwon/NewArquivoOpiniao/env3.8/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1337, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "/home/mwon/NewArquivoOpiniao/env3.8/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1337, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "/home/mwon/NewArquivoOpiniao/env3.8/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1263, in prepare_value
raise ValueError(
ValueError: Failed to insert expression "SearchVector(Col(accounts_author, accounts.Author.name), weight=A)" on accounts.Author.search_vector. F() expressions can only be used to update, not to insert.
Django version: 3.0.11

F() expressions can only be used to update, not to insert.
I'm no expert on VectorField, but maybe the weight='A' is causing the F() expression. Does it work without weight, as shown in the tutorial you refer to?
Otherwise, try an update:
Author.objects.all().update(search_vector=SearchVector('name', weight='A'))

The error message is saying that you are trying to insert a value into the search_vector field when creating a new object of Author, but the SearchVector class only supports updating existing objects, not creating new ones. To solve this, you can change the implementation of the save method to update the search_vector field after the object has been created:
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
Author.objects.filter(pk=self.pk).update(search_vector=SearchVector('name', weight='A'))
This way, you can create the object first, and then update the vector.

Related

how do i solve the following Django serializer error: TypeError: int() argument must be a string, a bytes-like object or a number, not 'Article'

Im quite new to django but i cant find the solution in the docs
My question:
I have a model with 2 OneToOne relations:
class Slotting(ICSBaseMixin):
# database fields
article = models.OneToOneField(
"ics_stock.Article",
on_delete=models.CASCADE,
related_name="slotting",
null=False
)
article_location = models.OneToOneField(
"ics_stock.ArticleLocation",
on_delete=models.CASCADE,
null=False,
related_name="slotting"
)
def __str__(self):
return str(self.record_id)
class Meta:
db_table = "ICS_Stock_Slotting"
verbose_name = "Slotting"
verbose_name_plural = "Slottings" # noqa
I want to create and save a new instance through a serializer.
serializer:
class SlottingCreateUpdateSerializer(serializers.ModelSerializer):
def update(self, instance, validated_data):
article = validated_data.pop("article")
instance.article_id = article
article_location = validated_data.pop("article_location")
instance.article_location_id = article_location
instance.save()
return instance
def create(self, validated_data):
article_id = validated_data.pop("article")
article = get_object_or_404(
Article,
record_id=article_id
)
article_location_id = validated_data.pop("article_location")
article_location = get_object_or_404(
ArticleLocation,
record_id=article_location_id
)
slotting = Slotting()
slotting.article_location = article_location,
slotting.article = article
slotting.save()
return slotting
Now when trying to serialize and save i get the following error:
raceback (most recent call last):
File "C:\000 Projects\Applications\ICS_Django_core\venv\lib\site-packages\django\db\models\fields\__init__.py", line 1988, in get_prep_value
return int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Article'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\000 Projects\Applications\ICS_Django_core\venv\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "C:\000 Projects\Applications\ICS_Django_core\venv\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\000 Projects\Applications\ICS_Django_core\venv\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\000 Projects\Applications\ICS_Django_core\venv\lib\site-packages\django\views\generic\base.py", line 84, in view
return self.dispatch(request, *args, **kwargs)
File "C:\000 Projects\Applications\ICS_Django_core\venv\lib\site-packages\rest_framework\views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "C:\000 Projects\Applications\ICS_Django_core\venv\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\000 Projects\Applications\ICS_Django_core\venv\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
raise exc
File "C:\000 Projects\Applications\ICS_Django_core\venv\lib\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "C:\000 Projects\Applications\ICS_Django_core\ics_stock\api\views\slotting.py", line 44, in post
instance = validated_data.save()
File "C:\000 Projects\Applications\ICS_Django_core\venv\lib\site-packages\rest_framework\serializers.py", line 212, in save
self.instance = self.create(validated_data)
File "C:\000 Projects\Applications\ICS_Django_core\ics_stock\api\serializers\slotting.py", line 40, in create
article = get_object_or_404(
File "C:\000 Projects\Applications\ICS_Django_core\venv\lib\site-packages\django\shortcuts.py", line 85, in get_object_or_404
return queryset.get(*args, **kwargs)
File "C:\000 Projects\Applications\ICS_Django_core\venv\lib\site-packages\django\db\models\query.py", line 482, in get
clone = self._chain() if self.query.combinator else self.filter(*args, **kwargs)
File "C:\000 Projects\Applications\ICS_Django_core\venv\lib\site-packages\django\db\models\query.py", line 1071, in filter
return self._filter_or_exclude(False, args, kwargs)
File "C:\000 Projects\Applications\ICS_Django_core\venv\lib\site-packages\django\db\models\query.py", line 1089, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, args, kwargs)
File "C:\000 Projects\Applications\ICS_Django_core\venv\lib\site-packages\django\db\models\query.py", line 1096, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File "C:\000 Projects\Applications\ICS_Django_core\venv\lib\site-packages\django\db\models\sql\query.py", line 1502, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\000 Projects\Applications\ICS_Django_core\venv\lib\site-packages\django\db\models\sql\query.py", line 1532, in _add_q
child_clause, needed_inner = self.build_filter(
File "C:\000 Projects\Applications\ICS_Django_core\venv\lib\site-packages\django\db\models\sql\query.py", line 1448, in build_filter
condition = self.build_lookup(lookups, col, value)
File "C:\000 Projects\Applications\ICS_Django_core\venv\lib\site-packages\django\db\models\sql\query.py", line 1273, in build_lookup
lookup = lookup_class(lhs, rhs)
File "C:\000 Projects\Applications\ICS_Django_core\venv\lib\site-packages\django\db\models\lookups.py", line 27, in __init__
self.rhs = self.get_prep_lookup()
File "C:\000 Projects\Applications\ICS_Django_core\venv\lib\site-packages\django\db\models\lookups.py", line 85, in get_prep_lookup
return self.lhs.output_field.get_prep_value(self.rhs)
File "C:\000 Projects\Applications\ICS_Django_core\venv\lib\site-packages\django\db\models\fields\__init__.py", line 1990, in get_prep_value
raise e.__class__(
TypeError: Field 'record_id' expected a number but got <Article: test article>.
Now im googling and looking for a whole day but i still dont know what im doing wrong.
What do i need to refactor to be able to fix the bug.
I created a serializer class to inherrit from that uses a dict input from the Meta class.
class RelatedModelSerializer(serializers.ModelSerializer):
"""
serializes all relationship based models
added field for Meta class
- relationships: dict[str:Model]
Create and update method adds all fields from relationships with the primary key with from model described in relationships dict and saves and raises HTTP404 when object not found
- Arjen Keller
"""
class Meta:
relationships = {}
model = None
def create(self, validated_data):
for key in frozenset(validated_data):
if self.Meta.relationships and key in self.Meta.relationships:
validated_data[f"{key}_id"] = get_object_or_404(
self.Meta.relationships[key],
record_id=validated_data.pop(key).id
).id
return self.Meta.model.objects.create(**validated_data)
def update(self, instance, validated_data):
for key in frozenset(validated_data):
setattr(
instance, f"{key}_id",
validated_data.pop(key)
) if self.Meta.relationships and key in self.Meta.relationships and get_object_or_404(
self.Meta.relationships[key],
record_id=validated_data[key].id
).id else setattr(
instance,
f"{key}",
validated_data.pop(key)
)
instance.save(**validated_data)
return instance
i hope it helps somebody.

Unclear errors when trying to save data while overriding default 'create' method

Tried to write custom create method for my model, but run into some unclear errors.
Here is my code:
# models.py:
class ItemModel(models.Model):
item_id = models.CharField(max_length=10, primary_key=True)
name = models.CharField(max_length=40)
active = models.BooleanField(default=True)
def __str__(self):
return self.item_id
class ItemVersion(models.Model):
item_ver_id = models.CharField(max_length=13, primary_key=True)
item_ver = models.TextField()
config = models.TextField()
model = models.ForeignKey(ItemModel, on_delete=models.CASCADE, default=0)
session_id = models.CharField(max_length=40, default=0)
creation_date = models.DateTimeField(auto_now=False, auto_now_add=True)
finished = models.BooleanField(default=False)
def name(self):
return self.model.name
def __str__(self):
return str(self.model)
# serializers.py:
class ItemModelSerializer(serializers.ModelSerializer):
item_id = serializers.RegexField(regex='^\d{3}-\d{9}$', allow_blank=False)
name = serializers.CharField(min_length=6, max_length=50, allow_blank=False)
class Meta:
model = ItemModel
fields = '__all__'
class ItemVersionSerializer(serializers.ModelSerializer):
item_ver_id = serializers.RegexField(regex='^r\d{2}$', allow_blank=False)
session_id = serializers.RegexField(regex='^s\d{2}$', allow_blank=False)
link = serializers.SerializerMethodField()
name = serializers.SerializerMethodField()
config = serializers.CharField(min_length=6)
item_ver = serializers.CharField(min_length=6)
def get_name(self, obj):
return obj.name()
def get_link(self, obj):
link = 'https://example.net/' + str(obj.model)
+ str('-dyn') + '/?iv_id=' + str(obj.item_ver_id)
+ '&sessid=' + str(obj.session_id)
return link
# views.py:
class ItemModelViewSet(viewsets.ModelViewSet):
queryset = ItemModel.objects.all()
serializer_class = ItemModelSerializer
lookup_field = 'item_id'
class ItemVersionViewSet(viewsets.ModelViewSet):
serializer_class = ItemVersionSerializer
lookup_field = 'item_ver_id'
def get_queryset(self):
pass
def create(self, request, *args, **kwargs):
data = request.data
model = ItemModel.objects.get(item_id=data["model"])
item_version = ItemVersion.objects.create(
# model=model,
item_ver_id=data["item_ver_id"],
config=data["config"],
item_ver=data["item_ver"],
session_id=data["session_id"]
# finished=data["finished"]
)
item_version.model.add(model)
finished = True if data["finished"] else False
item_version.finished.add(finished)
item_version.save()
serializer = ItemVersionSerializer(item_version)
return Response(data)
For some reason, I keep getting FOREIGN KEY constraint failed and the session_id=data["session_id"] line is highlighted as the one where problem occurs nearby.
Any ideas how to solve this?
Edit: traceback:
Traceback (most recent call last):
File "/home/aqv/workspace/django_rest_fw/ct_test/environ/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/aqv/workspace/django_rest_fw/ct_test/environ/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: FOREIGN KEY constraint failed
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/aqv/workspace/django_rest_fw/ct_test/environ/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/aqv/workspace/django_rest_fw/ct_test/environ/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/aqv/workspace/django_rest_fw/ct_test/environ/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/aqv/workspace/django_rest_fw/ct_test/environ/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/aqv/workspace/django_rest_fw/ct_test/environ/lib/python3.6/site-packages/rest_framework/viewsets.py", line 116, in view
return self.dispatch(request, *args, **kwargs)
File "/home/aqv/workspace/django_rest_fw/ct_test/environ/lib/python3.6/site-packages/rest_framework/views.py", line 495, in dispatch
response = self.handle_exception(exc)
File "/home/aqv/workspace/django_rest_fw/ct_test/environ/lib/python3.6/site-packages/rest_framework/views.py", line 455, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/aqv/workspace/django_rest_fw/ct_test/environ/lib/python3.6/site-packages/rest_framework/views.py", line 492, in dispatch
response = handler(request, *args, **kwargs)
File "/home/aqv/workspace/django_rest_fw/ct_test/core/views.py", line 47, in create
session_id=data["session_id"]
File "/home/aqv/workspace/django_rest_fw/ct_test/environ/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/aqv/workspace/django_rest_fw/ct_test/environ/lib/python3.6/site-packages/django/db/models/query.py", line 422, in create
obj.save(force_insert=True, using=self.db)
File "/home/aqv/workspace/django_rest_fw/ct_test/environ/lib/python3.6/site-packages/django/db/models/base.py", line 741, in save
force_update=force_update, update_fields=update_fields)
File "/home/aqv/workspace/django_rest_fw/ct_test/environ/lib/python3.6/site-packages/django/db/models/base.py", line 779, in save_base
force_update, using, update_fields,
File "/home/aqv/workspace/django_rest_fw/ct_test/environ/lib/python3.6/site-packages/django/db/models/base.py", line 870, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/aqv/workspace/django_rest_fw/ct_test/environ/lib/python3.6/site-packages/django/db/models/base.py", line 908, in _do_insert
using=using, raw=raw)
File "/home/aqv/workspace/django_rest_fw/ct_test/environ/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/aqv/workspace/django_rest_fw/ct_test/environ/lib/python3.6/site-packages/django/db/models/query.py", line 1186, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/aqv/workspace/django_rest_fw/ct_test/environ/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1332, in execute_sql
cursor.execute(sql, params)
File "/home/aqv/workspace/django_rest_fw/ct_test/environ/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute
return super().execute(sql, params)
File "/home/aqv/workspace/django_rest_fw/ct_test/environ/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/aqv/workspace/django_rest_fw/ct_test/environ/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/aqv/workspace/django_rest_fw/ct_test/environ/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/aqv/workspace/django_rest_fw/ct_test/environ/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/aqv/workspace/django_rest_fw/ct_test/environ/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/aqv/workspace/django_rest_fw/ct_test/environ/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: FOREIGN KEY constraint failed
The only foreign key I see is model there, and in your code you aren't passing it.
def create():
model = ItemModel.objects.get(item_id=data["model"])
item_version = ItemVersion.objects.create(
# model=model,
...
This will cause the FK constraint issue if you do not pass in a valid model instance or id because:
You have a default=0 for the model field
But no ItemModel with pk=0 exists in the databsae
If you want model to be nullable, then you can just add that to the FK definition:
class ItemVersion(models.Model):
...
model = models.ForeignKey(ItemModel, null=True, on_delete=CASCADE)
Later on I see you have these 2 lines:
item_version.model.add()
item_version.finished.add(finished).
These are both incorrect. add() doesn't work on boolean model field, and the .add() for an FK is only valid for many-to-many FKs, which are not being used here. The way you are passing them in the commented out sections is fine.
You can get a default value for the 'finished' flag by saying:
data.get('finished', False)
# this will throw a KeyError if "finished" isn't in the dict
True if data["finished"] else False
# this will not throw an error (but doesn't check the value of finished)
True if "finished" in data else False
Some other notes:
1) You use a ModelSerializer without a Meta class inside. Consider just using a standard serializer if you really want to do it by hand, or read up on ModelSerializers. If you use it correctly you shouldn't need a custom create method in the viewset.
2) default=<anything> is not a good idea on an FK. An FK should not generally have a default value (though there are some cases where its nice, like with pre-defined system data in constant tables)
3) You aren't using a serializer in your create method. You are accessing request.data directly. This will give you no validation, and no ability to say finished=BooleanField(default=False) and always get a value for serializer.validated_data['finished'].

Retrieve ForeignKey from views before saving in DJANGO

How can i retrieve the foreign key from the form child relationship, save it at the same time that I am saving the parent information on the same page.
I am currently working with two forms, the Parent and the child. The logic of the page is saving the name of a book and the name of the course it contains.
Ex: Book = "Learn data structure and algorithms",
Language = "Python"
models.py
class Entity(models.Model):
entity_name = models.CharField(max_length=250, blank=False)
owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name='users')
class Meta:
verbose_name_plural = "Entities"
def __str__(self):
return self.entity_name
def get_absolute_url(self):
return reverse('snippets:password', kwargs={'pk': self.pk})
class Password(models.Model):
password_name = models.CharField(max_length=12, blank=False)
entity = models.ForeignKey(Entity, on_delete=models.CASCADE, related_name='passwords')
def __str__(self):
return self.password_name
forms.py
from django import forms
from snippets.models import Entity, Password
class EntityForm(forms.ModelForm):
class Meta:
model = Entity
fields = ('entity_name',)
class PasswordForm(forms.ModelForm):
class Meta:
model = Password
fields = ('password_name',)
views.py
def password_creation(request):
if request.method == 'POST':
form_one = EntityForm(request.POST)
form_two = PasswordForm(request.POST)
if form_one.is_valid():
entity_form = form_one.save(commit=False)
entity_form.owner = request.user
entity_form.save()
password_form = form_two.save(commit=False)
# HERE : what is the right way to retrieve name from PK
# owner is not part the Password table field therefore
# need to be changed to 'password_name'or 'entity'
# then, how to retrieve the field from the view??
password_form.owner = request.user
password_form.save()
return redirect('snippets:entity')
else:
form_one = EntityForm()
form_two = PasswordForm()
context = {
'form_one':form_one,
'form_two':form_two,
}
return render(request, 'snippets/create-password.html', context)
I have tried the codes above. The problem is that Django is saving the name of the "Book" but not the name of the "Language" in the database. The system also breaks and return this message : NOT NULL constraint failed: snippets_password.entity_id.
Any help would be much appreciated.
Traceback error
Traceback (most recent call last):
File "/Users/macadmin/Documents/Django_wapps/password_change_test/lib/python3.7/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/Users/macadmin/Documents/Django_wapps/password_change_test/lib/python3.7/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/macadmin/Documents/Django_wapps/password_change_test/lib/python3.7/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/macadmin/Documents/Django_wapps/password_change_test/password_change/snippets/views.py", line 67, in password_creation
password_form.save()
File "/Users/macadmin/Documents/Django_wapps/password_change_test/lib/python3.7/site-packages/django/db/models/base.py", line 729, in save
force_update=force_update, update_fields=update_fields)
File "/Users/macadmin/Documents/Django_wapps/password_change_test/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/macadmin/Documents/Django_wapps/password_change_test/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/macadmin/Documents/Django_wapps/password_change_test/lib/python3.7/site-packages/django/db/models/base.py", line 880, in _do_insert
using=using, raw=raw)
File "/Users/macadmin/Documents/Django_wapps/password_change_test/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/macadmin/Documents/Django_wapps/password_change_test/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/macadmin/Documents/Django_wapps/password_change_test/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1280, in execute_sql
cursor.execute(sql, params)
File "/Users/macadmin/Documents/Django_wapps/password_change_test/lib/python3.7/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/Users/macadmin/Documents/Django_wapps/password_change_test/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/macadmin/Documents/Django_wapps/password_change_test/lib/python3.7/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/Users/macadmin/Documents/Django_wapps/password_change_test/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/Users/macadmin/Documents/Django_wapps/password_change_test/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/macadmin/Documents/Django_wapps/password_change_test/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/Users/macadmin/Documents/Django_wapps/password_change_test/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 303, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: snippets_password.entity_id
Your diagnosis is not at all correct. Password doesn't have an owner field, or in fact any relation with User. Its relationship is with Entity, and the error clearly states that it is the entity or that is missing. So you just have to assign the Entity you just created:
if form_one.is_valid() and form_two.is_valid():
entity = form_one.save(commit=False)
entity.owner = request.user
entity.save()
password = form_two.save(commit=False)
password.entity = entity
password.save()
Note, I added a check that that form_two is valid, and also renamed the objects: the result of calling form.save is not a form, but an instance of the model.

NoneType object is not callable when trying to save instance of Model

I am using Django to perform a very simple function. The application will grow in size as time goes on, but for now, all I want to do is run through an html document of all my Facebook messages, and save a model for every user, and every message attached to that user. However when trying to create an instance of a model I created, FacebookUser, I get the error "NoneType" object is not callable. I read other SO articles, and browsed the internet for other issues, and found that the error usually stemmed from trying to do something with the class itself, and not an instantiation of the class. However I am creating an instance of this class, and still getting this error.
models.py
from django.db import models
class FacebookUser(models.Model):
full_name = models.CharField(max_length=255, null=True, blank=True)
def __str__(self):
return self.full_name
class Message(models.Model):
user = models.ForeignKey(FacebookUser)
content = models.TextField(max_length=10000, null=True, blank=True)
date_sent = models.DateTimeField(null=True, blank=True)
def __str__(self):
return '#{} {}'.format(self.id, self.user.full_name)
views.py
from django.http import HttpResponse
from facebook_user.models import FacebookUser, Message
from bs4 import BeautifulSoup
def crawl_messages(request):
data = open('messages.html', 'r').read()
soup = BeautifulSoup(data)
all_messages = soup.findAll("div", {"class": "message"})
msg_dictionaries = [{
'user': el.findAll('span')[0],
'time': el.findAll('span')[1],
'content': el.nextSibling.nextSibling
} for el in all_messages]
for msg in msg_dictionaries:
try:
fbuser = FacebookUser.objects.get(full_name=msg['user'])
print('Try One')
print(fbuser)
except FacebookUser.DoesNotExist:
fbuser = FacebookUser.objects.create(full_name=msg['user'])
print('Try Two')
print(fbuser)
except Exception as e:
print('Try Three')
print(e)
fbuser = None
if fbuser:
new_msg = Message()
new_msg.content = msg['content']
new_msg.user = fbuser
new_msg.save()
return HttpResponse('Worked ! Check the Admin')
Stack trace
Traceback (most recent call last):
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/timothybaney/Data Science/facebook_sentiment_analysis/facebook_user/views.py", line 19, in crawl_messages
fbuser = FacebookUser.objects.create(full_name=msg['user'])
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/db/models/query.py", line 401, in create
obj.save(force_insert=True, using=self.db)
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/db/models/base.py", line 708, in save
force_update=force_update, update_fields=update_fields)
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/db/models/base.py", line 736, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/db/models/base.py", line 820, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/db/models/base.py", line 859, in _do_insert
using=using, raw=raw)
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/db/models/query.py", line 1039, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/db/models/sql/compiler.py", line 1059, in execute_sql
for sql, params in self.as_sql():
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/db/models/sql/compiler.py", line 1019, in as_sql
for obj in self.query.objs
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/db/models/sql/compiler.py", line 1019, in <listcomp>
for obj in self.query.objs
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/db/models/sql/compiler.py", line 1018, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "/Users/timothybaney/Library/Python/3.5/lib/python/site-packages/django/db/models/sql/compiler.py", line 946, in prepare_value
value = value.resolve_expression(self.query, allow_joins=False, for_save=True)
TypeError: 'NoneType' object is not callable
This most likely means that msg['user'] is not a string, but an object that sets any attribute when that attribute is called. And thus it receives resolve_expression attribute that causes above error.
For a fix, try casting msg['user'] to a string in your create statement:
fbuser = FacebookUser.objects.create(str(msg['user'])
This is just a guess, without full source to run it there's no way to be certain.

How to update some data when post object in Django Rest?

I have this Serializer:
class LikeSerializer(serializers.ModelSerializer):
user = serializers.PrimaryKeyRelatedField(queryset=ExtUser.objects.all(), required=False, allow_null=True, default=None)
class Meta:
model = Like
field = ('user', 'post')
def create(self, validated_data):
post = Post(id=validated_data['post'], author=validated_data['user'])
#post=Post.objects.get(pk=validated_data['post'])
post.rating += 1
print(post)
post.save()
return Like.objects.create(**validated_data)
And I get error, when i trying to save Like object TypeError: int() argument must be a string or a number, not 'Post'
Trying to add int(), but it not helps, I think I not correct update Post object
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/rest_framework/views.py", line 466, in dispatch
response = self.handle_exception(exc)
File "/Library/Python/2.7/site-packages/rest_framework/views.py", line 463, in dispatch
response = handler(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/rest_framework/decorators.py", line 53, in handler
return func(*args, **kwargs)
File "/Users/olegklimakov/Documents/6rp/likes/views.py", line 19, in send_like
serializer.save(user=request.user, isLiked=True)
File "/Library/Python/2.7/site-packages/rest_framework/serializers.py", line 191, in save
self.instance = self.create(validated_data)
File "/Users/olegklimakov/Documents/6rp/likes/serializers.py", line 39, in create
post.save()
File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 700, in save
force_update=force_update, update_fields=update_fields)
File "/Library/Python/2.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 "/Library/Python/2.7/site-packages/django/db/models/base.py", line 793, in _save_table
forced_update)
File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 823, in _do_update
filtered = base_qs.filter(pk=pk_val)
File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 790, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 808, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py", line 1243, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py", line 1269, in _add_q
allow_joins=allow_joins, split_subq=split_subq,
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py", line 1203, in build_filter
condition = self.build_lookup(lookups, col, value)
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py", line 1099, in build_lookup
return final_lookup(lhs, rhs)
File "/Library/Python/2.7/site-packages/django/db/models/lookups.py", line 19, in __init__
self.rhs = self.get_prep_lookup()
File "/Library/Python/2.7/site-packages/django/db/models/lookups.py", line 57, in get_prep_lookup
return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 744, in get_prep_lookup
return self.get_prep_value(value)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 976, in get_prep_value
return int(value)
TypeError: int() argument must be a string or a number, not 'Post'
Post model:
class Post(models.Model):
id = models.AutoField(primary_key=True)
date_create = models.DateField(auto_now_add=True)
date_change = models.DateField(auto_now=True)
title = models.CharField(max_length=250) # Max Length of title = 250
description = models.CharField(max_length=1000) # Max Length = 1000 chars
rating = models.IntegerField(default=1)
author = models.ForeignKey('extuser.ExtUser', blank=True, null=True)
def __unicode__(self):
return u"%s" % self.id
It seems like inside the statement
"Post(id=validated_data['post'], author=validated_data['user'])",
validated_data['post'] gives you a Post object, not a int object,
but the 'id' inside the Post model expect a int object.
That probably is the reason you get the error.
#transactional
def create(self, validated_data):
post = Post(id=validated_data['post'], author=validated_data['user'])
post.rating += 1
post.save()
return super(self, LikeSerializer).create(validated_data)
Its good to let ModelSerializer create do its job, also note you definitely want to use transaction to keep data consistent
Also, code below creates new Post entity:
post = Post(...)
post.save()
You definitely need to get existent Post entity instead
Post.objects.filter(...).first()