django删除问题 - 使用OneToOneField关系删除多个对象 - 获取MultipleObjectsReturned错误 [英] django delete problem - deleting more than one object with a OneToOneField relationship - got MultipleObjectsReturned error

查看:893
本文介绍了django删除问题 - 使用OneToOneField关系删除多个对象 - 获取MultipleObjectsReturned错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个删除我的对象的问题。我写了一个删除方法,获取要删除的对象的ID列表。这对对象和具有外键的对象工作正常,但如果我与要删除的对象具有OneToOneField关系,则失败。



这是我的删除方法:

  @login_required 
def delete_objects(request,model,selected_ids):
'''
封装批量删除方法
删除给定模型找到的所有对象
默认失败,因为model.delete()始终默认失败
''
object_list = model.objects .filter(pk__in = selected_ids)
count = object_list.count()
如果count == 1:
name = model._meta.verbose_name.title()
else:
name = model._meta.verbose_name_plural.title()
object_list.delete()
request.user.message_set.create(message ='成功删除%s%s'%(计数,名称))
return

到目前为止,我只是使用联系人对象进行测试。现在我添加一个PhoneNumber模型到我的模型。 PhoneNumber模型与Contact模型具有OneToOneField关系。如果没有PhoneNumber对象或一个PhoneNumber对象分配给Contact对象,我可以删除它。但是如果我将多个PhoneNumber对象与一个Contact对象关联,我会收到一个错误。



这是我收到的错误消息:

  MultipleObjects返回/ crm / contacts / 
get()返回多个PhoneNumber - 它返回3!查询参数为{'contact__pk':4L}
请求方法:POST
请求URL:http://127.0.0.1:8000/crm/contacts/
异常类型:MultipleObjectsReturned
异常值:
get()返回多个PhoneNumber - 它返回3!查询参数是{'contact__pk':4L}

我在django文档中阅读



删除对象



任何具有指向要删除的对象的外键的对象将一起删除。这是我想要完成的目标..但现在我收到一个错误。我想要的是要删除的对象:D



其实这可能是一个设计问题?当我将多个PhoneNumber对象与OneToOneField关系的Contact对象关联时,是否出错?我选择了OneToOneField,因为电话号码是唯一的,应该只与一个联系人有关。

解决方案


当我将多个PhoneNumber对象与OneToOneField关系的Contact对象关联时,是不是有问题?


更改与PhoneNumber中的外键的关系,并为 PhoneNumberField 设置唯一选项。



应该这样结束:

  class PhoneNumber(models.Model):
#some fields ...
number = PhoneNumberField(unique = True)
contact = models.ForeignKey(Contact,related_name =phone_numbers)
pre>

另外,为了确保我了解你想要的约束设置:这个例子假设一个电话号码只会在整个数据库中出现一次,因此也是只有一个联系人可以拥有该电话号码。



如果您想要的限制较少,请使用 unique_together 模型的Meta选项如下所示:

  class PhoneNumber(models.Model):
#some fields ...
number = PhoneNumberField()
contact = models.ForeignKey(Contact,related_name =phone_numbers)

cla ss Meta:
unique_together =(number,contact)

这将允许多个联系人具有相同的电话号码,但是一个联系人不能多于一次。



http://docs.djangoproject.com/en/dev/ref/models/options/#unique-together


I have a problem with deleting my objects. I wrote a delete method which get a list of IDs of objects to be deleted. This works fine for objects and for objects with foreign keys, but it fails if i have a OneToOneField relationship to the object which i want to delete.

This is my delete method:

@login_required
def delete_objects(request, model, selected_ids):
    '''
    capsulate a bulk delete method
    delete all objects found for the given model
    fails silently since model.delete() always fails silently
    '''
    object_list = model.objects.filter(pk__in=selected_ids)
    count = object_list.count()
    if count == 1:
        name = model._meta.verbose_name.title()
    else:
        name = model._meta.verbose_name_plural.title()
    object_list.delete()
    request.user.message_set.create(message='Successfully deleted %s %s' % (count,name))
    return

Till now I just tested it with Contact objects. Now I added a PhoneNumber model to my models. The PhoneNumber model has a OneToOneField relationship to the Contact model. If no PhoneNumber object or one PhoneNumber object is assigned to the Contact object i can delete it. But if I relate more than one PhoneNumber object to a Contact object i get an error.

This is the error message that i get:

MultipleObjectsReturned at /crm/contacts/
get() returned more than one PhoneNumber -- it returned 3! Lookup parameters were {'contact__pk': 4L}
Request Method: POST
Request URL:    http://127.0.0.1:8000/crm/contacts/
Exception Type: MultipleObjectsReturned
Exception Value:    
get() returned more than one PhoneNumber -- it returned 3! Lookup parameters were {'contact__pk': 4L}

I read in the django docs

deleting objects

that "any objects which had foreign keys pointing at the object to be deleted will be deleted along with it." This is the goal I want to accomplish.. but now i get an error. All I want is the objects to be deleted :D

Actually is this maybe a design problem? Is it wrong when I relate more than one PhoneNumber object to a Contact object with a OneToOneField relationship? I have chosen OneToOneField since the phone number is unique and should be related to only one contact.

解决方案

Is it wrong when I relate more than one PhoneNumber object to a Contact object with a OneToOneField relationship?

You got it. Change the relationship to a foreign key in PhoneNumber and set the unique option for the PhoneNumberField.

Should end up like this:

class PhoneNumber(models.Model):
    # some fields...
    number = PhoneNumberField(unique=True)
    contact = models.ForeignKey(Contact, related_name="phone_numbers")

Also, just to make sure I understand how you want the constraints set up: This example assumes that a phone number will only appear once in the entire database and therefore also that only one contact can have that phone number.

In case you wanted something less restrictive, use unique_together in the model's Meta options like so:

class PhoneNumber(models.Model):
    # some fields...
    number = PhoneNumberField()
    contact = models.ForeignKey(Contact, related_name="phone_numbers")

class Meta:
    unique_together = ("number", "contact")

This will allow multiple contacts to have the same phone number, but a single contact cannot have that number more than once.

http://docs.djangoproject.com/en/dev/ref/models/options/#unique-together

这篇关于django删除问题 - 使用OneToOneField关系删除多个对象 - 获取MultipleObjectsReturned错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆