我的CustomUser模型中有一个OneToOne关系。 我的模特是:
class Channel(models.Model):
channel_name = models.CharField(max_length=60, primary_key=True)
channel_host = models.CharField(max_length=60)
channel_secret = models.CharField(max_length=60)
profile_type = models.ForeignKey(Profile)
channel_register_time = models.DateField(auto_now_add=True)
channel_creator = models.CharField(max_length=60)
class CustomUser(AbstractUser):
channel = models.OneToOneField(Channel, unique=True, db_index=True, primary_key=True)
register_time = models.DateField(auto_now_add=True)
cellphone = models.CharField(max_length=60)
fax = models.CharField(max_length=60)
tell = models.CharField(max_length=60)
picture = models.ImageField(upload_to='media/', max_length=100)
当我创建一个新用户时,每件事都很好,但当我尝试更改channl字段时,我收到此错误:
In [93]: c1 = Channel.objects.create(channel_name='channel1',profile_type=p)
In [94]: c2 = Channel.objects.create(channel_name='channel2',profile_type=p)
In [95]: u = CustomUser.objects.create(username='django',channel=c1)
In [96]: u.channel = c2
In [97]: u.save()
---------------------------------------------------------------------------
IntegrityError Traceback (most recent call last)
<ipython-input-97-5d13c143bca2> in <module>()
----> 1 u.save()
/usr/lib/python2.7/site-packages/django/db/models/base.pyc in save(self, force_insert, force_update, using, update_fields)
543
544 self.save_base(using=using, force_insert=force_insert,
--> 545 force_update=force_update, update_fields=update_fields)
546 save.alters_data = True
547
/usr/lib/python2.7/site-packages/django/db/models/base.pyc in save_base(self, raw, force_insert, force_update, using, update_fields)
571 if not raw:
572 self._save_parents(cls, using, update_fields)
--> 573 updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
574 # Store the database on which the object was saved
575 self._state.db = using
/usr/lib/python2.7/site-packages/django/db/models/base.pyc in _save_table(self, raw, cls, force_insert, force_update, using, update_fields)
652
653 update_pk = bool(meta.has_auto_field and not pk_set)
--> 654 result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
655 if update_pk:
656 setattr(self, meta.pk.attname, result)
/usr/lib/python2.7/site-packages/django/db/models/base.pyc in _do_insert(self, manager, using, fields, update_pk, raw)
685 """
686 return manager._insert([self], fields=fields, return_id=update_pk,
--> 687 using=using, raw=raw)
688
689 def delete(self, using=None):
/usr/lib/python2.7/site-packages/django/db/models/manager.pyc in _insert(self, objs, fields, **kwargs)
230
231 def _insert(self, objs, fields, **kwargs):
--> 232 return insert_query(self.model, objs, fields, **kwargs)
233
234 def _update(self, values, **kwargs):
/usr/lib/python2.7/site-packages/django/db/models/query.pyc in insert_query(model, objs, fields, return_id, raw, using)
1509 query = sql.InsertQuery(model)
1510 query.insert_values(fields, objs, raw=raw)
-> 1511 return query.get_compiler(using=using).execute_sql(return_id)
1512
1513
/usr/lib/python2.7/site-packages/django/db/models/sql/compiler.pyc in execute_sql(self, return_id)
896 cursor = self.connection.cursor()
897 for sql, params in self.as_sql():
--> 898 cursor.execute(sql, params)
899 if not (return_id and cursor):
900 return
/usr/lib/python2.7/site-packages/django/db/backends/util.pyc in execute(self, sql, params)
67 start = time()
68 try:
---> 69 return super(CursorDebugWrapper, self).execute(sql, params)
70 finally:
71 stop = time()
/usr/lib/python2.7/site-packages/django/db/backends/util.pyc in execute(self, sql, params)
51 return self.cursor.execute(sql)
52 else:
---> 53 return self.cursor.execute(sql, params)
54
55 def executemany(self, sql, param_list):
/usr/lib/python2.7/site-packages/django/db/utils.pyc in __exit__(self, exc_type, exc_value, traceback)
97 if dj_exc_type not in (DataError, IntegrityError):
98 self.wrapper.errors_occurred = True
---> 99 six.reraise(dj_exc_type, dj_exc_value, traceback)
100
101 def __call__(self, func):
/usr/lib/python2.7/site-packages/django/db/backends/util.pyc in execute(self, sql, params)
51 return self.cursor.execute(sql)
52 else:
---> 53 return self.cursor.execute(sql, params)
54
55 def executemany(self, sql, param_list):
/usr/lib/python2.7/site-packages/django/db/backends/sqlite3/base.pyc in execute(self, query, params)
448 return Database.Cursor.execute(self, query)
449 query = self.convert_query(query)
--> 450 return Database.Cursor.execute(self, query, params)
451
452 def executemany(self, query, param_list):
IntegrityError: column username is not unique
我错过了什么吗?如何更改频道字段?
答案 0 :(得分:1)
这是因为channel
模型上的CustomUser
字段有primary_key=True
。
这意味着当你这样做:
In [96]: u.channel = c2
In [97]: u.save()
...您已更改u
的主键,因此在保存时创建 new 用户,但用户名与原始用户名相同,因此存在IntegrityError
我认为你需要这样做:
u = CustomUser.objects.get(username='django')
u.delete()
u.channel = c2
u.save()