Skip to content

Instantly share code, notes, and snippets.

@kezabelle
Created June 12, 2013 13:34
Show Gist options
  • Save kezabelle/5765278 to your computer and use it in GitHub Desktop.
Save kezabelle/5765278 to your computer and use it in GitHub Desktop.
Apparently Django's add_to_class can magically sidestep the inability to specify fields of the same name (potentially across models?)
>>> from uuid import uuid4
>>> from django.db import models
>>> from django.contrib.auth.models import User
>>> User.objects.create(username=str(uuid4()))
<User: >
>>> User.add_to_class('username', models.CharField(max_length=300))
>>> User.objects.create(username=str(uuid4()))
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/django/db/models/manager.py", line 137, in create
return self.get_query_set().create(**kwargs)
File "/django/db/models/query.py", line 377, in create
obj.save(force_insert=True, using=self.db)
File "/django/db/models/base.py", line 463, in save
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/django/db/models/base.py", line 551, in save_base
result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/django/db/models/manager.py", line 203, in _insert
return insert_query(self.model, objs, fields, **kwargs)
File "/django/db/models/query.py", line 1593, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/django/db/models/sql/compiler.py", line 912, in execute_sql
cursor.execute(sql, params)
File "/django/db/backends/util.py", line 40, in execute
return self.cursor.execute(sql, params)
File "/django/db/backends/mysql/base.py", line 114, in execute
return self.cursor.execute(query, args)
File "/MySQLdb/cursors.py", line 201, in execute
self.errorhandler(self, exc, value)
File "/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
DatabaseError: (1110, "Column 'username' specified twice")
@kezabelle
Copy link
Author

Tested against:

Django==1.4.5 
Django==1.5.1
MySQL-python==1.2.4

Also tried against the GitHub zipball as of django/django@92c49d6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment