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)