• 1000 - он вернул 2!
Как устранить эту ошибку?
models.py:
from django.db import models
import datetime
import pytz
from django.utils import timezone
class c1(models.Model):
name = models.CharField(max_length=30, default="Other")
def __str__(self):
return self.name
class c2(models.Model):
c1 = models.ForeignKey(c1, on_delete=models.CASCADE)
name = models.CharField(max_length=30, default="Other")
def __str__(self):
return self.name
class c3(models.Model):
c2 = models.ForeignKey(c2, on_delete=models.CASCADE)
name = models.CharField(max_length=30, default="Other")
def __str__(self):
return self.name
class Task_manager(models.Manager):
def create_Task(self, title):
Task1 = self.create(title=title)
return Task1
class Task(models.Model):
STATUS = (
('ONGOING', 'ONGOING'),
('COMPLETED','COMPLETED'),
)
search=models.CharField(max_length=300, choices=search_title, default="OTHER")
c1=models.ForeignKey(c1, on_delete=models.SET_NULL, null=True)
c2=models.ForeignKey(c2, on_delete=models.SET_NULL, null=True)
c3=models.ForeignKey(c3, on_delete=models.SET_NULL, null=True)
title=models.CharField(max_length=30,default="Other")
created=models.DateTimeField(default=timezone.now())
objects=Task_manager()
class Meta:
ordering = [ '-created']
def __str__(self):
return self.title
Вот мой класс ModelAmdin в admin.py admin.py snippet:
class TaskAdmin(admin.ModelAdmin):
film_status.allow_tags = True'''
form=TaskForm
list_filter=('c1')
readonly_fields = ['score','score_fixed','deadline_fixed','created']
def change_view(self,request,object_id,extra_content=None):
return super(TaskAdmin,self).change_view(request,object_id)
admin.site.register(Task,TaskAdmin)
admin.site.register(c1)
admin.site.register(c2)
admin.site.register(c3)
Вы также можете посмотреть класс TaskForm. forms.py:
class TaskForm(forms.ModelForm):
class Meta:
model=Task
fields='__all__'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['c1'].queryset = c1.objects.all()
self.fields['c2'].queryset = c2.objects.get()
self.fields['c3'].queryset = c3.objects.none()
Вот полная ошибка для кода:
Traceback (most recent call last):
File "C:\Users\sarda\anaconda3\envs\myDjangoEnv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\sarda\anaconda3\envs\myDjangoEnv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\sarda\anaconda3\envs\myDjangoEnv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\sarda\anaconda3\envs\myDjangoEnv\lib\site-packages\django\contrib\admin\options.py", line 607, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Users\sarda\anaconda3\envs\myDjangoEnv\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\sarda\anaconda3\envs\myDjangoEnv\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "C:\Users\sarda\anaconda3\envs\myDjangoEnv\lib\site-packages\django\contrib\admin\sites.py", line 231, in inner
return view(request, *args, **kwargs)
File "C:\My_Django_stuff\tada\todo\tasks\admin.py", line 64, in change_view
return super(TaskAdmin,self).change_view(request,object_id)
File "C:\Users\sarda\anaconda3\envs\myDjangoEnv\lib\site-packages\django\contrib\admin\options.py", line 1641, in change_view
return self.changeform_view(request, object_id, form_url, extra_context)
File "C:\Users\sarda\anaconda3\envs\myDjangoEnv\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "C:\Users\sarda\anaconda3\envs\myDjangoEnv\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\sarda\anaconda3\envs\myDjangoEnv\lib\site-packages\django\contrib\admin\options.py", line 1522, in changeform_view
return self._changeform_view(request, object_id, form_url, extra_context)
File "C:\Users\sarda\anaconda3\envs\myDjangoEnv\lib\site-packages\django\contrib\admin\options.py", line 1582, in _changeform_view
form = ModelForm(instance=obj)
File "C:\My_Django_stuff\tada\todo\tasks\forms.py", line 14, in __init__
self.fields['c2'].queryset = c2.objects.get()
File "C:\Users\sarda\anaconda3\envs\myDjangoEnv\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\sarda\anaconda3\envs\myDjangoEnv\lib\site-packages\django\db\models\query.py", line 419, in get
raise self.model.MultipleObjectsReturned(
Exception Type: MultipleObjectsReturned at /admin/tasks/task/9/change/
Exception Value: get() returned more than one c2 -- it returned 2!