django south migration with custom field not working - django

All,
I have several custom fields on my models. I have tried adding "south_field_triple" methods on them in order to get db migration working. Initializing my application with south ("python manage.py convert_so_south myApp") works. But generating the first migration ("python manage.py schemamigration myApp --auto") fails with the following error:
TypeError: type() takes 1 or 3 arguments
The problem is occurring with the following field (I am not showing the code for the custom FormField or Widget; I assume those are not related to the cause of the problem):
class MyCustomField(models.CharField):
_type = "EnumerationField"
enumerationAppName = ""
enumerationModelName = ""
def __init__(self,*args,**kwargs):
enumeration = kwargs.pop('enumeration',None)
super(MyCustomField,self).__init__(*args,**kwargs)
if enumeration:
(self.enumerationAppName, self.enumerationModelName) = enumeration.split(".")
def getEnumeration(self):
try:
app = get_app(self.enumerationAppName)
enumeration = getattr(app,self.enumerationModelName)
return enumeration
except:
msg = "failed to get enumeration '%s.%s'" % (self.enumerationAppName,self.enumerationModelName)
print "error: %s" % msg
return None
def south_field_triple(self):
field_class_path = self.__class__.__module__ + "." + self.__class__.__name__
args,kwargs = introspector(self)
return (field_class_path,args,kwargs)
For what it's worth, this field presents the user with a set of choices. Those choices are defined in another class (specified by the "enumeration" argument to init). The FormField and Widget associated with this field use MultiValueField and MultiWidget, respectively, to present the user with a combo-box and a separate text box where the user can enter their own custom value not in the original enumeration. However, in the case of the model in the application being migrated - an enumeration is not provided.
Any ideas on what's gone wrong? Thanks.
edit: stacktrace added
File "manage.py", line 11, in <module>
execute_manager(settings)
File "/usr/local/lib/python2.7/site-packages/Django-1.4-py2.7.egg/django/core/management/__init__.py", line 459, in execute_manager
utility.execute()
File "/usr/local/lib/python2.7/site-packages/Django-1.4-py2.7.egg/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/site-packages/south/management/commands/schemamigration.py", line 97, in handle
old_orm = last_migration.orm(),
File "/usr/local/lib/python2.7/site-packages/south/utils.py", line 62, in method
value = function(self)
File "/usr/local/lib/python2.7/site-packages/south/migration/base.py", line 422, in orm
return FakeORM(self.migration_class(), self.app_label())
File "/usr/local/lib/python2.7/site-packages/south/orm.py", line 46, in FakeORM
_orm_cache[args] = _FakeORM(*args)
File "/usr/local/lib/python2.7/site-packages/south/orm.py", line 126, in __init__
self.models[name] = self.make_model(app_label, model_name, data)
File "/usr/local/lib/python2.7/site-packages/south/orm.py", line 320, in make_model
field = self.eval_in_context(code, app, extra_imports)
File "/usr/local/lib/python2.7/site-packages/south/orm.py", line 238, in eval_in_context
return eval(code, globals(), fake_locals)
File "<string>", line 1, in <module>
TypeError: type() takes 1 or 3 arguments

Aha! Turns out it was another custom field causing the problem; One that was created using a factory method.
class MyOtherCustomField(models.Field):
def __init__(self,*args,**kwargs):
super(MyOtherCustomField,self).__init__(**kwargs)
#classmethod
def Factory(cls,model_field_class_name,**kwargs):
try:
# there is a global dictionary of potential field_classes and extra kwargs to pass to the constructor
model_field_class_info = MODELFIELD_MAP[model_field_class_name.lower()]
model_field_class = model_field_class_info[0]
model_field_class_kwargs = model_field_class_info[1]
except KeyError:
msg = "unknown field type: '%s'" % model_field_class_name
print "error: %s" % msg
raise MyError(msg)
class _MyOtherCustomField(cls,model_field_class):
def __init__(self,*args,**kwargs):
kwargs.update(model_field_class_kwargs)
super(_MyOtherCustomField,self).__init__(**kwargs)
self._type = model_field_class_name
def south_field_triple(self):
# I was doing this which didn't work...
#field_class_path = self.__class__.__module__ + "." + self.__class__.__name__
# I'm now doing this which does work...
field_class_path = "django.db.models.fields" + "." + model_field_class.__name__
args,kwargs = introspector(self)
return (field_class_path,args,kwargs)
return _MyOtherCustomField(**kwargs)

Related

convert-engine issue with saving any thumbnail

So I have a model like this
from django.db import models
from sorl.thumbnail import get_thumbnail
class Upload(BaseModel):
#staticmethod
def upload_path_handler(instance, filename):
return f'boxes/{instance.box.id}/uploads/{filename}'
#staticmethod
def thumbnail_path_handler(instance, filename):
return f'boxes/{instance.box.id}/thumbnails/{filename}'
def save(self, *args, **kwargs):
if self._state.adding:
# we cache the file size and store
# it into the database to improve performance
# we cannot edit the object's file so we don't
# bother to modify the file size on updates
self.size = self.file.size
super(Upload, self).save(*args, **kwargs)
thumbnail = get_thumbnail(self.file, '1280x720', crop='center')
# sorl is not saving the thumbnails for non-image files
return self.thumbnail.save(thumbnail.name, ContentFile(thumbnail.read()), True)
super(Upload, self).save(*args, **kwargs)
objects = api_managers.UploadManager()
size = models.PositiveBigIntegerField()
name = models.CharField(max_length=100, default='untitled', validators=[MinLengthValidator(2)])
channel = models.ForeignKey('api_backend.Channel', on_delete=models.CASCADE, editable=False)
box = models.ForeignKey('api_backend.Box', on_delete=models.CASCADE, editable=False)
owner = models.ForeignKey('api_backend.User', on_delete=models.CASCADE, editable=False)
thumbnail = models.ImageField(max_length=512, upload_to=thumbnail_path_handler.__func__, null=True, blank=True)
file = models.FileField(max_length=512, upload_to=upload_path_handler.__func__)
REQUIRED_FIELDS = [file, owner]
the file field can be literally any file, and I want sorl-thumbnail to make a thumbnail for the same and save it into the thumbnail field. I am on windows and am using ImageMagick. [python version- 32 bits]
this is the binary distribution I installed.
https://imagemagick.org/script/download.php
ImageMagick-7.0.10-61-Q16-x86-dll.exe Win32 dynamic at 16 bits-per-pixel component
settings.py
THUMBNAIL_ENGINE = 'sorl.thumbnail.engines.convert_engine.Engine'
However, whenever an upload-model is saved, I get the following error.
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\iyapp\\PycharmProjects\\rebox\\media\\cache\\db\\5a\\db5a88e1d6a08cdfa1afbc92e9b8cb47.jpg'
Full traceback:
Exception ignored in: <function TemporaryFile.__del__ at 0x04184610>
Traceback (most recent call last):
File "C:\Users\iyapp\Envs\rebox_django\lib\site-packages\django\core\files\temp.py", line 61, in __del__
self.close()
File "C:\Users\iyapp\Envs\rebox_django\lib\site-packages\django\core\files\temp.py", line 49, in close
if not self.close_called:
AttributeError: 'TemporaryFile' object has no attribute 'close_called'
__init__() got an unexpected keyword argument 'delete'
Traceback (most recent call last):
File "C:\Users\iyapp\Envs\rebox_django\lib\site-packages\sorl\thumbnail\base.py", line 104, in get_thumbnail
source_image = default.engine.get_image(source)
File "C:\Users\iyapp\Envs\rebox_django\lib\site-packages\sorl\thumbnail\engines\convert_engine.py", line 76, in get_image
with NamedTemporaryFile(mode='wb', delete=False) as fp:
TypeError: __init__() got an unexpected keyword argument 'delete'
Remote file [boxes/2/uploads/a243bfbd00fdcb54982faf63cfc290b1dfcd47f1c0484facbd67c8b8ff606aff.jpg] at [1280x720] does not exist
exc: [Errno 2] No such file or directory: 'C:\\Users\\iyapp\\PycharmProjects\\rebox\\media\\cache\\db\\5a\\db5a88e1d6a08cdfa1afbc92e9b8cb47.jpg'
Internal Server Error: /api/channels/1/uploads/
Traceback (most recent call last):
File "C:\Users\iyapp\Envs\rebox_django\lib\site-packages\asgiref\sync.py", line 339, in thread_handler
raise exc_info[1]
File "C:\Users\iyapp\Envs\rebox_django\lib\site-packages\django\core\handlers\exception.py", line 38, in inner
response = await get_response(request)
File "C:\Users\iyapp\Envs\rebox_django\lib\site-packages\django\core\handlers\base.py", line 233, in _get_response_async
response = await wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\iyapp\Envs\rebox_django\lib\site-packages\asgiref\sync.py", line 304, in __call__
ret = await asyncio.wait_for(future, timeout=None)
File "c:\python38\lib\asyncio\tasks.py", line 455, in wait_for
return await fut
File "c:\python38\lib\concurrent\futures\thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "C:\Users\iyapp\Envs\rebox_django\lib\site-packages\asgiref\sync.py", line 343, in thread_handler
return func(*args, **kwargs)
File "C:\Users\iyapp\Envs\rebox_django\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\iyapp\Envs\rebox_django\lib\site-packages\django\views\generic\base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\iyapp\Envs\rebox_django\lib\site-packages\rest_framework\views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "C:\Users\iyapp\Envs\rebox_django\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\Users\iyapp\Envs\rebox_django\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
raise exc
File "C:\Users\iyapp\Envs\rebox_django\lib\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "C:\Users\iyapp\Envs\rebox_django\lib\site-packages\rest_framework\generics.py", line 242, in post
return self.create(request, *args, **kwargs)
File "C:\Users\iyapp\Envs\rebox_django\lib\site-packages\rest_framework\mixins.py", line 19, in create
self.perform_create(serializer)
File "C:\Users\iyapp\Envs\rebox_django\lib\site-packages\rest_framework\mixins.py", line 24, in perform_create
serializer.save()
File "C:\Users\iyapp\Envs\rebox_django\lib\site-packages\rest_framework\serializers.py", line 205, in save
self.instance = self.create(validated_data)
File "C:\Users\iyapp\PycharmProjects\rebox\api_backend\serializers\partial.py", line 35, in create
return super(PartialUploadSerializer, self).create(validated_data)
File "C:\Users\iyapp\Envs\rebox_django\lib\site-packages\rest_framework\serializers.py", line 939, in create
instance = ModelClass._default_manager.create(**validated_data)
File "C:\Users\iyapp\Envs\rebox_django\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\iyapp\Envs\rebox_django\lib\site-packages\django\db\models\query.py", line 447, in create
obj.save(force_insert=True, using=self.db)
File "C:\Users\iyapp\PycharmProjects\rebox\api_backend\models\uploads.py", line 43, in save
return self.thumbnail.save(thumbnail.name, ContentFile(thumbnail.read()), True)
File "C:\Users\iyapp\Envs\rebox_django\lib\site-packages\sorl\thumbnail\images.py", line 162, in read
f = self.storage.open(self.name)
File "C:\Users\iyapp\Envs\rebox_django\lib\site-packages\django\core\files\storage.py", line 36, in open
return self._open(name, mode)
File "C:\Users\iyapp\Envs\rebox_django\lib\site-packages\django\core\files\storage.py", line 231, in _open
return File(open(self.path(name), mode))
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\iyapp\\PycharmProjects\\rebox\\media\\cache\\db\\5a\\db5a88e1d6a08cdfa1afbc92e9b8cb47.jpg'
Can someone please help me fix this?
thanks a lot!
as far as I know this is why the first exception arises.
from django.core.files.temp import NamedTemporaryFile
https://github.com/jazzband/sorl-thumbnail/blob/master/sorl/thumbnail/engines/convert_engine.py#L8
this import returns a TemporaryFile
(see source code)
"""
The temp module provides a NamedTemporaryFile that can be reopened in the same
process on any platform. Most platforms use the standard Python
tempfile.NamedTemporaryFile class, but Windows users are given a custom class.
This is needed because the Python implementation of NamedTemporaryFile uses the
O_TEMPORARY flag under Windows, which prevents the file from being reopened
if the same flag is not provided [1][2]. Note that this does not address the
more general issue of opening a file for writing and reading in multiple
processes in a manner that works across platforms.
The custom version of NamedTemporaryFile doesn't support the same keyword
arguments available in tempfile.NamedTemporaryFile.
1: https://mail.python.org/pipermail/python-list/2005-December/336957.html
2: https://bugs.python.org/issue14243
"""
import os
import tempfile
from django.core.files.utils import FileProxyMixin
__all__ = ('NamedTemporaryFile', 'gettempdir',)
if os.name == 'nt':
class TemporaryFile(FileProxyMixin):
"""
Temporary file object constructor that supports reopening of the
temporary file in Windows.
Unlike tempfile.NamedTemporaryFile from the standard library,
__init__() doesn't support the 'delete', 'buffering', 'encoding', or
'newline' keyword arguments.
"""
def __init__(self, mode='w+b', bufsize=-1, suffix='', prefix='', dir=None):
fd, name = tempfile.mkstemp(suffix=suffix, prefix=prefix, dir=dir)
self.name = name
self.file = os.fdopen(fd, mode, bufsize)
self.close_called = False
# Because close can be called during shutdown
# we need to cache os.unlink and access it
# as self.unlink only
unlink = os.unlink
def close(self):
if not self.close_called:
self.close_called = True
try:
self.file.close()
except OSError:
pass
try:
self.unlink(self.name)
except OSError:
pass
def __del__(self):
self.close()
def __enter__(self):
self.file.__enter__()
return self
def __exit__(self, exc, value, tb):
self.file.__exit__(exc, value, tb)
NamedTemporaryFile = TemporaryFile
else:
NamedTemporaryFile = tempfile.NamedTemporaryFile
gettempdir = tempfile.gettempdir
and the TemporayFile class' init method doesn't take any parameter named delete. instead, only tempfile.NamedTemporaryFile does. Therefore, this chunk of code fails.
def get_image(self, source):
"""
Returns the backend image objects from a ImageFile instance
"""
with NamedTemporaryFile(mode='wb', delete=False) as fp:
fp.write(source.read())
return {'source': fp.name, 'options': OrderedDict(), 'size': None}
https://github.com/jazzband/sorl-thumbnail/blob/master/sorl/thumbnail/engines/convert_engine.py#L72
I think that because of this the file isn't being saved at all.
And at last, inside the save method of the model,
We see the backend raise that the file doesn't exist.

How to create the tables in a model FlaskDB with peewee?

Going off the documentation here.
I have this code:
from playhouse.flask_utils import FlaskDB,
app = Flask(__name__)
app.config.from_object(__name__)
flask_db = FlaskDB(app)
database = flask_db.database
class Item(flask_db.Model):
title = CharField()
content = TextField()
category = CharField()
#app.route('/create',methods=('GET','POST'))
def create():
if request.method == 'POST':
if request.form.get('title') and request.form.get('content'):
item = Item.create(
title = request.form['title'],
content = request.form['content'],
category = request.form['category'])
flash('Item created successfully','success')
return redirect(url_for('view'),item=item)
else:
flash('Title and Content are required.','danger')
form = ItemForm()
return render_template('create.html',form=form)
if __name__ == '__main__':
database.create_tables(Item)
app.run(debug=True)
They say it:
Dynamically create a Peewee database instance based on app config data.
However, I believe I still need to create the tables, in fact when I tried it without doing that second to last line, I could see that no tables existed in the created blog.db file. Unfortunately when I run this now, I get:
Traceback (most recent call last):
sqliteext:////Users/conduce-laptop/PycharmProjects/alexmarshall.website/blog.db
File "/Users/conduce-laptop/PycharmProjects/alexmarshall.website/website2.py", line 73, in <module>
database.create_tables(Item)
File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/peewee.py", line 3855, in create_tables
create_model_tables(models, fail_silently=safe)
File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/peewee.py", line 5293, in create_model_tables
for m in sort_models_topologically(models):
File "playhouse/_speedups.pyx", line 341, in playhouse._speedups.sort_models_topologically (playhouse/_speedups.c:7091)
File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/peewee.py", line 4862, in __iter__
return iter(self.select())
File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/peewee.py", line 3240, in __iter__
return iter(self.execute())
File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/peewee.py", line 3233, in execute
self._qr = ResultWrapper(model_class, self._execute(), query_meta)
File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/peewee.py", line 2912, in _execute
return self.database.execute_sql(sql, params, self.require_commit)
File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/peewee.py", line 3775, in execute_sql
self.commit()
File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/peewee.py", line 3598, in __exit__
reraise(new_type, new_type(*exc_args), traceback)
File "/Users/conduce-laptop/anaconda2/lib/python2.7/site-packages/peewee.py", line 3768, in execute_sql
cursor.execute(sql, params or ())
peewee.OperationalError: no such table: item
You were invoking the create_tables incorrectly:
database.create_tables([Item], True)
The True allows you to call it multiple times without errors.
I had to run: Item.create_table(fail_silently=True) in in the very Bottom instead of database.create_tables(Item). Apparently you just run it once (since you wouldn't want to recreate the table

Integrity error - get_or_create says cannot be NULL on importing data

I asked a question to try and fix an issue I was having where I didn't understand how to use ForeignKeys properly. That was very helpfully fixed, although I had a subsequent problem with django.db.utils.IntegrityError: core_team.blahblah_id may not be NULL and I decided to roll back, do something slightly simpler - in order to avoid a double lookup (Match is linked to both Team and League), I would write a management command to import the teams.
On my (clearly flawed) understanding from the previous question, I've done it right - it uses get_or_create to check for the league instance and then assigns the team based on that. I've also doublechecked that the DB is up to date (I'm running south and did the forward migration the last time I changed the scheme, nothing's changed since then. Last change was to make the names in both models the primary key (as there's only one team of each name, only one league of each name.)
Most recently, I've added code to provide the default to the team get_or_create section, but am receiving the same error. I understand the cause (I think) of the error - that the ForeignKey 'league' in Team already exists in the database, and can't be null inserting another team (from https://docs.djangoproject.com/en/1.5/ref/models/querysets/#get-or-create), just not how to fix it.
Management command:
from django.core.management.base import BaseCommand, CommandError
import csv
import csvImporter
#from core.models import Match
from time import strptime
from datetime import datetime
master_data = open ('/Users/chris/Desktop/AllDataTruncated.csv', 'r')
data = list(tuple(rec) for rec in csv.reader(master_data, delimiter=','))
from core.models import League, Team
team_list = []
for row in data:
if row[2] == "HomeTeam":
print "Continuing"
continue
elif row[2] == "":
print "Continuing"
continue
else:
league, _ = League.objects.get_or_create(name=row[0])
print league
team, _ = Team.objects.get_or_create(team_name=row[2], defaults={'league':league})
current_team = Team(league = league, team_name=team)
print current_team
And relevant bits of models.py:
class League (models.Model):
name = models.CharField(max_length=2, primary_key=True)
last_modified = models.CharField(max_length=50)
def __unicode__(self):
return unicode(self.name)
class Team(models.Model):
team_name = models.CharField(max_length=50, primary_key=True)
league = models.ForeignKey(League)
team_colour = models.CharField(max_length=6, null=True, blank=True)
def __unicode__(self):
return unicode (self.team_name)
The full traceback is:
$ python manage.py importteams
Continuing
E0
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 272, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 77, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Users/chris/Dropbox/Django/gmblnew/core/management/commands/importteams.py", line 26, in <module>
team2, _ = Team.objects.get_or_create(team_name=row[3])
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/manager.py", line 146, in get_or_create
return self.get_query_set().get_or_create(**kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 487, in get_or_create
six.reraise(*exc_info)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 477, in get_or_create
obj.save(force_insert=True, using=self.db)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/base.py", line 546, in save
force_update=force_update, update_fields=update_fields)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/base.py", line 650, in save_base
result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/manager.py", line 215, in _insert
return insert_query(self.model, objs, fields, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 1661, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 937, in execute_sql
cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/util.py", line 41, in execute
return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 364, in execute
six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 362, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: core_team.league_id may not be NULL
Now - I think that it's odd that it's saying league_id here, as this shouldn't be relevant anymore? When I did the migration, the question came up:
? The field 'League.id' does not have a default specified, yet is NOT NULL.
? Since you are removing this field, you MUST specify a default
? value to use for existing rows. Would you like to:
? 1. Quit now.
? 2. Specify a one-off value to use for existing columns now
? 3. Disable the backwards migration by raising an exception; you can edit the migration to fix it later
? Please select a choice: 3
Is this what's perpetuating this issue?
Edit: Seems not. Dropped the DB and moved the South migrations folder, and it's still doing it. The source CSV is also fine (no blank lines or empty strings/segments), and the code above has a section to skip those segments anyway; it's not getting that far.
Ugh. The answer to this, for any other newbies who are coming to it later, is actually ludicrously simple. What I'm doing here is creating an entry in the table 'Team', which has a ForeignKey going back to 'League'.
The 'trick' (it's not a trick, just really badly explained in the documentation, imho) is that you need to explicitly pass the league back when you do the get_or_create for the Team object. It's not just about matching the team name
I thought I'd done this, but I hadn't, it appears. This code works (and quite effectively ensures there are no duplicates):
for row in data:
if row[2] == "HomeTeam":
print "Continuing"
continue
elif row[2] == "":
print "Continuing"
continue
else:
league, _ = League.objects.get_or_create(name=row[0])
print league
team, _ = Team.objects.get_or_create(team_name=row[2], league=league)
current_team = Team(league = league, team_name=team)
print current_team

__init__() takes at least 2 arguments (1 given) When Migrating using South with Custom Fields

i am new to south and i followed their documentation and after initializing south migrations, after running
manage.py migrate appname
for the following custom models Models i added introspection rules as follows
Models:
class DependentIntegerField(models.IntegerField):
def __init__(self, default_callable, *args, **kwargs):
self.default_callable = default_callable
super(DependentIntegerField, self).__init__(*args, **kwargs)
def pre_save(self, model_instance, add):
if not add:
return super(DependentIntegerField, self).pre_save(model_instance, add)
return self.default_callable(model_instance)
class Level(models.Model):
group = models.ForeignKey(Level_Group)
number = models.IntegerField(unique=True)#null=True, blank=True
threshold = DependentIntegerField(lambda mi:mi.number*50,null=False,blank=True)
def __str__(self):
return '%s' %(self.number)
def get_fib(self):
return fib(self.number+3)
class Gallery (models.Model):
contractor = models.ForeignKey(Contractor)
image = StdImageField(upload_to='GalleryDB', size=(640, 480,True))
Title = models.CharField(max_length=250,null=True,blank = True)
Caption = models.CharField(max_length=1000,null=True,blank=True)
Introspection Rules :
add_introspection_rules([
(
[Level], # Class(es) these apply to
[], # Positional arguments (not used)
{ # Keyword argument
"threshold": ["threshold", {}],
},
),
], ["^shoghlanah\.models\.DependentIntegerField"])
add_introspection_rules([
(
[Gallery], # Class(es) these apply to
[], # Positional arguments (not used)
{ # Keyword argument
"image": ["image"], "upload_to": ["GalleryDB"]
},
),
], ["^stdimage\.fields\.StdImageField"])
Traceback
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 232, in execute
output = self.handle(*args, **options)
File "/Library/Python/2.7/site-packages/South-0.7.5-py2.7.egg/south/management/commands/migrate.py", line 107, in handle
ignore_ghosts = ignore_ghosts,
File "/Library/Python/2.7/site-packages/South-0.7.5-py2.7.egg/south/migration/__init__.py", line 219, in migrate_app
success = migrator.migrate_many(target, workplan, database)
File "/Library/Python/2.7/site-packages/South-0.7.5-py2.7.egg/south/migration/migrators.py", line 235, in migrate_many
result = migrator.__class__.migrate_many(migrator, target, migrations, database)
File "/Library/Python/2.7/site-packages/South-0.7.5-py2.7.egg/south/migration/migrators.py", line 310, in migrate_many
result = self.migrate(migration, database)
File "/Library/Python/2.7/site-packages/South-0.7.5-py2.7.egg/south/migration/migrators.py", line 133, in migrate
result = self.run(migration)
File "/Library/Python/2.7/site-packages/South-0.7.5-py2.7.egg/south/migration/migrators.py", line 99, in run
south.db.db.current_orm = self.orm(migration)
File "/Library/Python/2.7/site-packages/South-0.7.5-py2.7.egg/south/migration/migrators.py", line 260, in orm
return migration.orm()
File "/Library/Python/2.7/site-packages/South-0.7.5-py2.7.egg/south/utils/__init__.py", line 62, in method
value = function(self)
File "/Library/Python/2.7/site-packages/South-0.7.5-py2.7.egg/south/migration/base.py", line 427, in orm
return FakeORM(self.migration_class(), self.app_label())
File "/Library/Python/2.7/site-packages/South-0.7.5-py2.7.egg/south/orm.py", line 45, in FakeORM
_orm_cache[args] = _FakeORM(*args)
File "/Library/Python/2.7/site-packages/South-0.7.5-py2.7.egg/south/orm.py", line 124, in __init__
self.models[name] = self.make_model(app_label, model_name, data)
File "/Library/Python/2.7/site-packages/South-0.7.5-py2.7.egg/south/orm.py", line 317, in make_model
field = self.eval_in_context(code, app, extra_imports)
File "/Library/Python/2.7/site-packages/South-0.7.5-py2.7.egg/south/orm.py", line 235, in eval_in_context
return eval(code, globals(), fake_locals)
File "<string>", line 1, in <module>
TypeError: __init__() takes at least 2 arguments (1 given)
i am little sure it's from the field DependentIntegerField but i don't know which __init__ is it trying to call and i tried
"threshold": ["threshold", {"ldefault_callable":default_callable}],
but i get
NameError: name 'default_callable' is not defined
and i have no clues how to fix this, any help is appreciated.
How to pass callable argument to field used in south
You have such field class and you want to make it work with south --- problem is with default_callable argument that is a callable and cannot be frozen.
class DependentIntegerField(models.IntegerField):
def __init__(self, default_callable, *args, **kwargs):
self.default_callable = default_callable
super(DependentIntegerField, self).__init__(*args, **kwargs)
Ignore this argument
What about leaving this argument altogether --- make it optional on the field and remove this argument from intrspection rules (so south with ignore it's existence) --- I gather that callable argument won't change database behaviour of field --- and that is the only thing south is concerned with. Either allow nulls in this argument (and make the field fail when performing database operations --- like saving model instance --- with null default_callable.
It will be OK during south run because south doesn't save any models --- it just creates database tables, and during normal operations model definitions will be taken from models.py file that will have default_callable set.
Pass information about what function to call
In some module create a dictionary od default_callable functions that are accessible by string keys.
Change your field so:
class DependentIntegerField(models.IntegerField):
def __init__(self, default_callable="foo.bar", *args, **kwargs):
self.default_callable = default_callable #it it string now!
super(DependentIntegerField, self).__init__(*args, **kwargs)
#property
def default_fun(self):
return registry[self.default_callable]
Where registry is this dictionary of all default functions.
Well south with custom fields can be a mess, introspection rules basically tell south what and how properties translate to constructor arguments. For example to constructor argument from ForeignKey translates to property field.rel.to. Moreover you define introspections for fields and not for models
The same with your code: when constructing constructor call to your field south must know that field property default_callable translates to constructor argument of the same name. So I gather that your introsbection rules should be defined like that:
add_introspection_rules([
(
[DependentIntegerField], # Notice it is for a field not a model
[], # Positional arguments (not used)
{ # Keyword argument
"default_callable": ["default_callable", {'default' : <<some defined constant>>}],
},
),
], ["^shoghlanah\.models\.DependentIntegerField"])
As for:
NameError: name 'default_callable' is not defined
i guess that function default_callable was not defined in the current scope.

Error while using date between in django raw query

I am always getting this error TypeError: not all arguments converted during string formatting
Here is my query
State.objects.raw('...review_create_date between %s and %s group by error_type',[fromdate,todate])
Here fromdate=2011-05-21 and todate='2011-05-27'
The above query executes in mysql prompt but could not able to run in python shell
Please some one help me
Here is the traceback
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 1412, in __iter__
query = iter(self.query)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/query.py", line 73, in __iter__
self._execute_query()
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/query.py", line 87, in _execute_query
self.cursor.execute(self.sql, self.params)
File "/usr/local/lib/python2.6/dist-packages/django/db/backends/util.py", line 18, in execute
sql = self.db.ops.last_executed_query(self.cursor, sql, params)
File "/usr/local/lib/python2.6/dist-packages/django/db/backends/__init__.py", line 216, in last_executed_query
return smart_unicode(sql) % u_params
TypeError: not all arguments converted during string formatting
django raw sql query in view
I also faced this issue, so I changes the old formatting to new using
format. I it works for me.
models.py
class VehicleDamage(models.Model):
requestdate = models.DateTimeField("requestdate")
vendor_name = models.CharField("vendor_name", max_length=50)
class Meta:
managed=False
views.py
def location_damageReports(request):
#static date for testing
date_from = '2019-11-01'
date_to = '2019-21-01'
vehicle_damage_reports = VehicleDamage.objects.raw("SELECT * FROM VendorReport_vehicledamage WHERE requestdate BETWEEN '{0}' AND '{1}'".format(date_from, date_to))
damage_report = DashboardDamageReportSerializer(vehicle_damage_reports, many=True)
data={"data": damage_report.data}
return HttpResponse(json.dumps(data), content_type="application/json")
Try using ? instead of %s.