0

MultipleObjectsReturned at /user/(name of object)/ Exception Type: MultipleObjectsReturned

Request Method: GET

Exception Value: get() returned more than one Canvas -- it returned 2!

Exception Location: C:\Users\user\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\query.py in get, line 389

Multiple objects with the same name, but a user can only have one unique object name.

Whenever I create another object with the same name I get this error (MultipleObjectsReturned). I want to allow every user to create one unique object name.

For example: user1 can have a unique object name of (test) and user2 can also have a unique object name of (test).

class Object(models.Model):
    user                = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
    object_name         = models.CharField(
                        max_length=100,
                        validators=[
                            # validate_canvas_title,
                            RegexValidator(
                                    regex=CANVAS_REGEX,
                                    message='Canvas must only contain Alpahnumeric characters',
                                    code='invalid_canvas_title'
                                )],
                        )
    slug                = models.SlugField(max_length=100, blank=True)

    class Meta:
        unique_together = ['user', 'object_name']

view

def canvasView(request, username=None, slug=None):
    user = User.objects.get(username__iexact=username)
    object = get_object_or_404(Object, slug__iexact=slug)

    template = "pages/object.html"
    context = {
        'user' : user,
        'object': object,
    }
    return render(request, template, context)

3 Answers 3

1

You need to use the user in the query.

object = get_object_or_404(Object, user=user, slug__iexact=slug)
Sign up to request clarification or add additional context in comments.

2 Comments

How would i get the object through the user. do i user user.object_set.all(), and then filter?
Yes you could do user.object_set.get(slug__iexact=slug), but then you would have to catch the ObjectNotFound exception and raise 404.
1

The get_object_or_404 function returns the object, which satisfies the condition specified, the thing to keep in my mind is that there should only be a single object with the specified condition. It doesn't return a list or anything like that. If multiple objects are found, then it returns the so called error.

You need to pass user into the arguments,

object_name = get_object_or_404(Object, user=user, slug__iexact=slug)

Or you could define a related name constraint in the model, for the user field,

user = models.ForeignKey(User, related_name="my_objects")

Then, you can get from the user objects directly,

user = User.objects.get(username=username)
object_name = user.my_objects.get(slug__iexact=slug)

Also, using python default keywords for naming variables are strictly not recommended. "object" is a valid python keyword. Its just not a good practice.

Comments

0

Set unique=True on the model field object_name.

2 Comments

but if i set it to unique then it won't allow me to have multiple objects with the same name.
This won't work, because 'user1 can have a unique object name of (test) and user2 can also have a unique object name of (test).' He could use unique_together = (("user", "object_name"),). See docs.djangoproject.com/en/1.11/ref/models/options/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.