我的models.py:
class MyFile(models.Model):
file = models.FileField(upload_to="myfiles", max_length=500, storage=OverwriteStorage())
slug = models.SlugField(max_length=500, blank=True)
user = models.ForeignKey(User, on_delete=models.SET_NULL, blank=True, null=True,)
date_created = models.DateTimeField(auto_now_add = True, blank=True, null=True)
date_expired = models.DateTimeField(default=default_time, blank=True, null=True)
expires = models.BooleanField(default=True)
def __str__(self):
return self.file.name
@models.permalink
def get_absolute_url(self):
return ('myfile:myfile-new', )
def save(self, *args, **kwargs):
self.slug = self.file.name
super(MyFile, self).save(*args, **kwargs)
def delete(self, *args, **kwargs):
self.file.delete(False)
super(MyFile, self).delete(*args, **kwargs)
class Meta:
managed = True
@receiver(pre_delete, sender=MyFile)
def remove_file(**kwargs):
instance = kwargs.get('instance')
instance.file.delete(save=False)
class Operation(models.Model):
id = models.AutoField(primary_key = True)
user = models.ForeignKey(User, blank=True, null=True)
myfile = models.ForeignKey(MyFile, blank=True, null=True)
def get_data(self):
return json.loads(self.data)
class Meta:
managed = True
class UserProfile(models.Model):
user = models.OneToOneField(User)
subscribe = models.BooleanField(default=True)
def __unicode__(self):
return u'Profile of user: %s' % (self.user.username)
class Meta:
managed = True
def create_user_profile(sender, instance, created, **kwargs):
from django.conf import settings
subscribe = False
if created:
profile, created = UserProfile.objects.get_or_create(user=instance, subscribe=subscribe)
post_save.connect(create_user_profile, sender=User)
但是,如果用户被删除,所有相关的myfiles仍然会被删除。
有什么想法吗?感谢
更新
数据库是postgres9.3.5,我使用\ d +来检查迁移前后表的信息,没有差异。有线。
# \d+ ui.myproject_myfile;
Table "ui.myproject_myfile"
Column | Type | Modifiers | Storage | Stats target | Description
--------------+--------------------------+------------------------------------------------------------------+----------+--------------+-------------
id | integer | not null default nextval('ui.myproject_myfile_id_seq'::regclass) | plain | |
file | character varying(500) | not null | extended | |
slug | character varying(500) | not null | extended | |
user_id | integer | | plain | |
date_created | timestamp with time zone | | plain | |
date_expired | timestamp with time zone | | plain | |
expires | boolean | not null | plain | |
Indexes:
"myproject_myfile_pkey" PRIMARY KEY, btree (id)
"myproject_myfile_2dbcba41" btree (slug)
"myproject_myfile_e8701ad4" btree (user_id)
Foreign-key constraints:
"myproject_myfile_user_id_a422765c7101118_fk_auth_user_id" FOREIGN KEY (user_id) REFERENCES ui.auth_user(id) DEFERRABLE INITIALLY DEFERRED
Referenced by:
TABLE "ui.myproject_operation" CONSTRAINT "myproject_ope_myfile_id_3f071e8e8361943b_fk_myproject_myfile_id" FOREIGN KEY (myfile_id) REFERENCES ui.myproject_myfile(id) DEFERRABLE INITIALLY DEFERRED
Has OIDs: no
更新
#ALTER TABLE ui.myproject_myfile ADD CONSTRAINT user_id_fk FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE SET NULL;
然后,我发现以下内容已添加到我的表格中。但仍然没有奏效。如果删除了用户,则删除所有myfiles。 " user_id_fk" FOREIGN KEY(user_id)REFERENCES ui.auth_user(id)ON DELETE SET NULL
更新
我正在考虑是否要删除此帖子。 VM重启后问题解决了。不知道为什么。但是,谢天谢地。
答案 0 :(得分:0)
on_delete
的默认行为导致所有相关对象被删除;在这种情况下,相关对象是MyFile。
如果您尝试保留MyFile对象,可以考虑通过on_delete=SET_DEFAULT
添加默认值或使用传递给on_default=SET(callable)
的可调用对象