from_db_value() takes 4 positional arguments but 5 were given Django - django

i have simple django notification table with the following structure
+-------------------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| level | varchar(20) | NO | | NULL | |
| unread | tinyint(1) | NO | | NULL | |
| actor_object_id | varchar(255) | NO | | NULL | |
| verb | varchar(255) | NO | | NULL | |
| description | longtext | YES | | NULL | |
| target_object_id | varchar(255) | YES | | NULL | |
| action_object_object_id | varchar(255) | YES | | NULL | |
| timestamp | datetime(6) | NO | | NULL | |
| public | tinyint(1) | NO | | NULL | |
| action_object_content_type_id | int(11) | YES | MUL | NULL | |
| actor_content_type_id | int(11) | NO | MUL | NULL | |
| recipient_id | int(11) | NO | MUL | NULL | |
| target_content_type_id | int(11) | YES | MUL | NULL | |
| deleted | tinyint(1) | NO | | NULL | |
| emailed | tinyint(1) | NO | | NULL | |
| data | longtext | YES | | NULL | |
And all what i want is to fetch the content so this is my view
#api_view(['GET'])
#login_required()
def getnotifications(request, page):
try:
if page == None:
page = 1
userID = request.user
unreadnum = Notification.objects.filter(recipient=request.user,
unread=True).count()
notifs = Notification.objects.filter(recipient=userID, unread=True).distinct().order_by(
'-timestamp')
print("got ntifs")
paginator = Paginator(notifs, 10)
paginatednotifs = paginator.page(page)
return Response(
{"notifications": NotificationSerializer(paginatednotifs,many=True, context={"user": request.user}).data,
"unread": unreadnum,"has_next":paginatednotifs.has_next()})
except Exception as e:
print("========")
print(str(e))
return Response(
{"notifications": str(e)})
and thus view's serilizer is like this :
class NotificationSerializer(serializers.ModelSerializer):
actor = serializers.SerializerMethodField()
target = serializers.SerializerMethodField()
class Meta:
model = Notification
fields = ("id","actor", "target","timestamp","verb")
def get_actor(self,obj):
user = Useraccount.objects.get(user__id=obj.actor_object_id)
return UserAccountSerializer(user,many=False,context={"user":self.context["user"]}).data
def get_target(self,obj):
if obj.target_content_type.model == "action":
action = ActstreamAction.objects.get(id=obj.target_object_id)
return ActionNotificationSerializer(action,many=False).data
return {"targetType":obj.target_content_type.model,"action":obj.action_object_content_type.model}
i tried to make many modifications in the serilizer and the view but always and always the same error
from_db_value() takes 4 positional arguments but 5 were given
i couldn't find this from_db_value() function
i'm really having a hard time with this problem, and i know just the basics about Django
i'm using
django : 1.11.18
djangorestframework : 3.6.4
mysql : 5.7.25
A traceback for the error:
Traceback (most recent call last):
File "<homedir>/project/webServer/app/myNotifications/views.py", line 66, in getnotifications
{"notifications": NotificationSerializer(paginatednotifs,many=True, context={"user": request.user}).data,
File "<homedir>/virtualenv/lib/python3.6/site-packages/rest_framework/serializers.py", line 739, in data
ret = super(ListSerializer, self).data
File "<homedir>/virtualenv/lib/python3.6/site-packages/rest_framework/serializers.py", line 263, in data
self._data = self.to_representation(self.instance)
File "<homedir>/virtualenv/lib/python3.6/site-packages/rest_framework/serializers.py", line 657, in to_representation
self.child.to_representation(item) for item in iterable
File "<homedir>/virtualenv/lib/python3.6/site-packages/rest_framework/serializers.py", line 657, in <listcomp>
self.child.to_representation(item) for item in iterable
File "/usr/lib/python3.6/_collections_abc.py", line 883, in __iter__
v = self[i]
File "<homedir>/virtualenv/lib/python3.6/site-packages/django/core/paginator.py", line 145, in __getitem__
self.object_list = list(self.object_list)
File "<homedir>/virtualenv/lib/python3.6/site-packages/django/db/models/query.py", line 250, in __iter__
self._fetch_all()
File "<homedir>/virtualenv/lib/python3.6/site-packages/django/db/models/query.py", line 1121, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "<homedir>/virtualenv/lib/python3.6/site-packages/django/db/models/query.py", line 62, in __iter__
for row in compiler.results_iter(results):
File "<homedir>/virtualenv/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 847, in results_iter
row = self.apply_converters(row, converters)
File "<homedir>/virtualenv/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 832, in apply_converters
value = converter(value, expression, self.connection, self.query.context)
TypeError: from_db_value() takes 4 positional arguments but 5 were given

TL;DR:
Most likely, jsonfield package is not incompatible with Django==1.11.18
Details:
You are using Django in version 1.11.18, which requires 5 positional arguments for from_db_value method and do not support JSONFields.
You are also using django-notifications package, which internally uses jsonfield>=1.0.3 package. Since there is no max. version set, django-notifications uses the newest version of jsonfield package.
The newest versions of jsonfield (3.0.0 and higher) doesn't support Django below 2.2. One of the reasons is that it takes only 4 arguments instead of 5.
The highest version of jsonfield that supports Django 1.11 is jsonfield==2.1.1
Please check the version of installed jsonfield package (use grep only if you're on unix sytem):
pip freeze | grep jsonfield
If it's 3.0.0 or more, you may try to downgrade it to 2.1.1. Be aware that it may (or may not) cause other compatibility issues with other packages.

I got the same error:
File "/home/django/lee3/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 833, in apply_converters
value = converter(value, expression, self.connection, self.query.context)
TypeError: from_db_value() takes 4 positional arguments but 5 were given
The 'from_db_value' it was complaining about was in /picklefield/fields.py.
Changed line 184:
def from_db_value(self, value, expression, connection):
to:
def from_db_value(self, value, expression, connection, context=None):
Everything works fine now.

Related

Django display related objects inside related objects in the admin

According to my assignment admin must be able to create Polls with Questions (create, delete, update) and Choices related to this questions. All of this should be displayed and changable on the same admin page.
Poll
|
|_question_1
| |
| |_choice_1(text)
| |
| |_choice_2
| |
| |_choice_3
|
|_question_2
| |
| |_choice_1
| |
| |_choice_2
| |
| |_choice_3
|
|_question_3
|
|_choice_1
|
|_choice_2
|
|_choice_3
Ok, it's not a problem to display one level of nesting like so on
class QuestionInline(admin.StackedInline):
model = Question
class PollAdmin(ModelAdmin):
inlines = [
QuestionInline,
]
But how to do to get the required poll design structure?
Check out this library it should provide the functionality.

Django can't find table, that exists in postgreSQL database

I'm new to database programming, apologies if I ask something simply.
I newly add few tables into my DB use Django model and migrations, now I'm using python bring data and print on scripts
Now to point of my error:
DB is connected successfully
Failed to execute database program
relation "cgi_limit" does not exist
LINE 1: SELECT * FROM CGI_limit
^
connection of DB had close successfully
now I check twice on naming. I try others tables such as auth_user and its was able print the table contents and I check to see if table exit in my DB as shown below;
Farm=# SELECT * FROM pg_tables;
schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity
public | django_session | FAT | | t | f | f | f
public | auth_permission | FAT | | t | f | t | f
public | auth_user_user_permissions | FAT | | t | f | t | f
public | auth_user | FAT | | t | f | t | f
public | django_admin_log | FAT | | t | f | t | f
public | CGI_ambient | FAT | | t | f | f | f
public | CGI_tank_system | FAT | | t | f | f | f
public | CGI_limit | FAT | | t | f | f | f
I my python code that render the DB;
#import liberys
import psycopg2 as pg2
from datetime import timedelta, datetime, date
############################################
# Function codes
def getDbConnection():
#Get Database connection
try:
connection =pg2.connect(user='FAT',
password='*******',
host='',
port='5432',
database='Farm')
print ("DB is connected succefully")
return connection
except(Exception, pg2.DatabaseError) as error:
print("Failed to connect to database")
def closeDbConnection(connection):
#Close Database connection
try:
connection.close()
print("connection of DB had close succefully")
except(Exception, pg2.DatabaseError) as error:
print("Failed to close database connection")
def DisplayDBdata():
try:
connection = getDbConnection()
cursor = connection.cursor()
query = 'SELECT * FROM "CGI_limit"'
cursor.execute(query,)
records = cursor.fetchall()
for row in records:
print("date: = ", row[1])
except(Exception, pg2.DatabaseError) as error:
print("Failed to execute database program")
print(error)
finally:
closeDbConnection(connection)
#############################################################
#code to be excuted
#DeleteDBdata()
DisplayDBdata() #for testing only
#end of code thats excute
I'm stump of what I should do. I did some google search and result only naming
I appreciate if you could help me
Postgres does not like capitalized table names. You will need to put the table name in quotes to make it work. I would recommend sticking with lowercase names.
query = 'SELECT * FROM "CGI_limit"'
Documentation link

vague doctrine exception when uploading file data (and store as longblob)

I've got a question regarding a mysterious doctrine query error.
Long story short: I'm trying to store longblob data within my database (which can go up to x00mb for example), so i did the following steps:
Create my own longblob type and field, register it according to:
https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/cookbook/advanced-field-value-conversion-using-custom-mapping-types.html
Doctrine custom data type
My MySQL database looks like: so i think it works?
mysql> describe DataBlocks;
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| data_type_id_id | int(11) | NO | MUL | NULL | |
| project_id_id | int(11) | NO | MUL | NULL | |
| data_block_name | varchar(100) | YES | | NULL | |
| content | longblob | YES | | NULL | |
| comment | longtext | YES | | NULL | |
| ts_added | datetime | NO | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
My Symfony4.1 FormType file field is as follows:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('dataBlockName', TextType::class)
->add('content', FileType::class)
I also adjusted the lines in my php.ini file for unlimited file size (i know this isn't really secure but.. it's just for now)
post_max_size = 0M
upload_max_filesize = 0M
And I get this error when my entity manager flushes the entity:
An exception occurred while executing 'INSERT INTO DataBlocks
(data_block_name, content, comment, ts_added, data_type_id_id,
project_id_id) VALUES (?, ?, ?, ?, ?, ?)' with params
["BTC_DOGE_tradehistory", Resource id #66, "450mb", "2018-10-08
10:19:44", 1, 1]:
Warning: Error while sending QUERY packet. PID=6016
Your help would be kindly appreciated!
FYI: it works for small files, but when i try to upload something big it becomes that vague error
The query describe in the exception tells you that the column content has for value a PHP resource. So i think it's a cast problem. Blob data are stored as bytes string. You also have possible issue with server configuration. There is Apache/Nginx or whatever, PHP but also the server sql.
Here an example for mysql: doc

Python 2.7 - insert text into a file before closing the file

I am writing some text into a file:
import codecs
outfile=codecs.open("c:/temp/myfile.sps","w+","utf-8-sig")
#procedures for creating the text_to_write
outfile.write (text_to_write)
outfile.close()
Now, what I want to do is to insert into the file an additional text, always at a certain line (say line 10), but this additional text is final only after all the procedures for creating the text_to_write. So the code for inserting the additional text, at line 10, should be the last code:
Is this possible without closing the file, reopening, and then saving again ?
(the reopen-insert-close approach is detailed here, but I would like to avoid it). I am looking for something like this:
import codecs
outfile=codecs.open("c:/temp/myfile.sps","w+","utf-8-sig")
#procedures for creating the text_to_write
outfile.write (text_to_write)
#code for inserting additional text at line 10
outfile.close()
Since you don't know the exact position (in bytes) of the insertion point, you need to read the lines of the file content, insert the additional text after the line 10 and write the file a second time.
note: a Python 2+3 way to open a file is to use the io module instead of the codecs module.
For instance, you have the following text to write and additional text:
text_to_write = u"""\
| 1 | This
| 2 |
| 3 | text
| 4 |
| 5 | contains
| 6 |
| 7 | at
| 8 |
| 9 | least
| 10 |
| 11 | ten
| 12 |
| 13 | lines."""
additional_text = u"""\
| ++ | ADDITIONAL
| ++ | TEXT
"""
You can open the file for reading and writing. The file is created if it does not
exist, otherwise it is truncated. The stream is positioned at
the beginning of the file.
with io.open("file.txt", mode="w+", encoding="utf-8-sig") as f:
f.write(text_to_write)
f.seek(0)
lines = f.readlines()
lines[10:10] = additional_text.splitlines(keepends=True)
f.seek(0)
f.writelines(lines)
This solution is not very efficient because you read the content you just write.
You can also process everything in memory and then write the file.
The result is:
| 1 | This
| 2 |
| 3 | text
| 4 |
| 5 | contains
| 6 |
| 7 | at
| 8 |
| 9 | least
| 10 |
| ++ | ADDITIONAL
| ++ | TEXT
| 11 | ten
| 12 |
| 13 | lines.
Another solution using a list in memory:
lines = text_to_write.splitlines(keepends=True)
lines[10:10] = additional_text.splitlines(keepends=True)
with io.open("file2.txt", mode="w+", encoding="utf-8-sig") as f:
f.writelines(lines)

Django model query behaving strangly

This is really weird, this query
originator = User.objects.get(Q(emailaddress__email__exact=fromfield) | Q(phonenumber__exact=fromfield))
returns two objects, when I do query separately then I get only one object. I can't see why is this happening :(
>>> originator = User.objects.get(emailaddress__email__exact=fromfield)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/site/gruppe/lib/python2.7/site-packages/django/db/models/manager.py", line 143, in get
return self.get_query_set().get(*args, **kwargs)
File "/usr/site/gruppe/lib/python2.7/site-packages/django/db/models/query.py", line 401, in get
(self.model._meta.object_name, kwargs))
DoesNotExist: GruppUser matching query does not exist. Lookup parameters were {'emailaddress__email__exact': 9135261967L}
>>> originator = User.objects.get(phonenumber__exact=fromfield)
>>> originator
<GruppUser: jorge>
>>> originator = User.objects.get(Q(emailaddress__email__exact=fromfield) | Q(phonenumber__exact=fromfield))
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/site/gruppe/lib/python2.7/site-packages/django/db/models/manager.py", line 143, in get
return self.get_query_set().get(*args, **kwargs)
File "/usr/site/gruppe/lib/python2.7/site-packages/django/db/models/query.py", line 405, in get
(self.model._meta.object_name, num, kwargs))
MultipleObjectsReturned: get() returned more than one GruppUser -- it returned 2! Lookup parameters were {}
And this is the queryset sql in raw:
(u'SELECT "gruppu_gruppuser"."password", "gruppu_gruppuser"."last_login", "gruppu_gruppuser"."is_superuser", "gruppu_gruppuser"."id", "gruppu_gruppuser"."username", "gruppu_gruppuser"."first_name", "gruppu_gruppuser"."last_name", "gruppu_gruppuser"."email", "gruppu_gruppuser"."is_staff", "gruppu_gruppuser"."is_active", "gruppu_gruppuser"."date_joined", "gruppu_gruppuser"."phonenumber", "gruppu_gruppuser"."mmsemail", "gruppu_gruppuser"."smsemail", "gruppu_gruppuser"."verified", "gruppu_gruppuser"."realname", "gruppu_gruppuser"."location", "gruppu_gruppuser"."website", "gruppu_gruppuser"."bio", "gruppu_gruppuser"."synctoFB" FROM "gruppu_gruppuser" LEFT OUTER JOIN "account_emailaddress" ON ("gruppu_gruppuser"."id" = "account_emailaddress"."user_id") WHERE ("account_emailaddress"."email" = %s OR "gruppu_gruppuser"."phonenumber" = %s )', (u'9135261967', u'9135261967'))
And it selects the same user:
>>> queryset.all()
[<GruppUser: jorge>, <GruppUser: jorge>]
The SQL really select two, this is black magic to me :( :
password | last_login | is_superuser | id | username | first_name | last_name | email | is_staff | is_active | date_joined | phonenumber | mmsemail | smsemail | verified | realname | location | website | bio | synctoFB
-------------------------------------------------------------------------------+-------------------------------+--------------+----+----------+------------+-----------+------------------+----------+-----------+------------------------+-------------+--------------------------+------------------------------------+----------+----------+----------+---------+-----+----------
xxxx | 2014-02-09 08:49:06.225694+04 | f | 2 | jorge | Jorge | Sanchez | x#x.c| f | t | 2013-10-08 14:53:16+04 | 9135261967 | 9135261967#xx | 9135261967#xx | t | | Prague | | | t
xxxx| 2014-02-09 08:49:06.225694+04 | f | 2 | jorge | Jorge | Sanchez | x#x.c | f | t | 2013-10-08 14:53:16+04 | 9135261967 | 9135261967#xx | 9135261967#xx | t | | Prague | | | t
(2 rows)
Try adding .distinct() to the end of your query. As described in the documentation .all() does not eliminate duplicate rows but .distinct() will.
Here's the relevant documentation:
https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.distinct