Форум сайта python.su
Делаю форму, где одно из полей - поле для выбора одного из вариантов (select).
Варианты - значения из БД одной из моделей. В БД внес 14 наименований для модели blog_cat.
class blog_cat(models.Model): class Meta(): db_table = "blog_Категории" blog_cat_name = models.CharField(unique=True, max_length=300, verbose_name="Название", help_text="Название") def __str__(self): return self.blog_cat_name class blogs(models.Model): class Meta(): db_table = "Блоги,рецепты,советы" blogs_title = models.CharField(max_length=300, verbose_name="Название Блога", help_text="Название Блога") blogs_cat = models.ForeignKey(blog_cat, verbose_name="Категория") blogs_user = models.ForeignKey(AuthUser, null=True, blank=True) blogs_date = models.DateTimeField(default=timezone.now()) blogs_text = models.TextField(max_length=113000, null=True, blank=True, verbose_name="Текст", help_text="Текст") blogs_like = models.IntegerField(default=0, verbose_name="Лайки", null=True, blank=True) blogs_looks = models.IntegerField(default=0, verbose_name="Просмотров", null=True, blank=True) def __str__(self): return self.blogs_title
[code python]class BlogForm(forms.ModelForm):
class Meta():
model = blogs
fields = ['blogs_title', 'blogs_cat', 'blogs_text']
widgets = {
'blogs_title': forms.TextInput(attrs={'placeholder': 'Название', 'max_length': '313'}),
'blogs_cat': forms.ModelChoiceField(queryset=blog_cat, empty_label=None, to_field_name="blog_cat_name"),
'blogs_text': forms.Textarea(attrs={'placeholder': 'Текст', 'cols': 50, 'rows': 15}),
}[/code]
[code python]C:\Python34\lib\site-packages\django-1.8.3-py3.4.egg\django\core\handlers\base.py in get_response
resolver_match = resolver.resolve(request.path_info) ...
? Local vars
C:\Python34\lib\site-packages\django-1.8.3-py3.4.egg\django\core\urlresolvers.py in resolve
for pattern in self.url_patterns: ...
? Local vars
C:\Python34\lib\site-packages\django-1.8.3-py3.4.egg\django\core\urlresolvers.py in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) ...
? Local vars
C:\Python34\lib\site-packages\django-1.8.3-py3.4.egg\django\core\urlresolvers.py in urlconf_module
self._urlconf_module = import_module(self.urlconf_name) ...
? Local vars
C:\Python34\lib\importlib\__init__.py in import_module
return _bootstrap._gcd_import(name[level:], package, level) ...
? Local vars
C:\Python34\nato4ka\nato4ka\urls.py in <module>
from blogs import urls ...
? Local vars
C:\Python34\nato4ka\blogs\urls.py in <module>
from blogs.views import * ...
? Local vars
C:\Python34\nato4ka\blogs\views.py in <module>
from blogs.forms import * ...
? Local vars
C:\Python34\nato4ka\blogs\forms.py in <module>
class BlogForm(forms.ModelForm): ...
? Local vars
C:\Python34\lib\site-packages\django-1.8.3-py3.4.egg\django\forms\models.py in __new__
opts.help_texts, opts.error_messages) ...
? Local vars
C:\Python34\lib\site-packages\django-1.8.3-py3.4.egg\django\forms\models.py in fields_for_model
formfield = f.formfield(**kwargs) ...
? Local vars
C:\Python34\lib\site-packages\django-1.8.3-py3.4.egg\django\db\models\fields\related.py in formfield
return super(ForeignKey, self).formfield(**defaults) ...
? Local vars
C:\Python34\lib\site-packages\django-1.8.3-py3.4.egg\django\db\models\fields\related.py in formfield
return super(RelatedField, self).formfield(**defaults) ...
? Local vars
C:\Python34\lib\site-packages\django-1.8.3-py3.4.egg\django\db\models\fields\__init__.py in formfield
return form_class(**defaults) ...
? Local vars
C:\Python34\lib\site-packages\django-1.8.3-py3.4.egg\django\forms\models.py in __init__
self.queryset = queryset ...
? Local vars
C:\Python34\lib\site-packages\django-1.8.3-py3.4.egg\django\forms\models.py in _set_queryset
self.widget.choices = self.choices ...
? Local vars
C:\Python34\lib\site-packages\django-1.8.3-py3.4.egg\django\forms\fields.py in _set_choices
value = list(value) ...
? Local vars
C:\Python34\lib\site-packages\django-1.8.3-py3.4.egg\django\forms\models.py in __iter__
yield self.choice(obj) ...
? Local vars
C:\Python34\lib\site-packages\django-1.8.3-py3.4.egg\django\forms\models.py in choice
return (self.field.prepare_value(obj), self.field.label_from_instance(obj)) ...
? Local vars
C:\Python34\lib\site-packages\django-1.8.3-py3.4.egg\django\forms\models.py in prepare_value
if self.to_field_name: ...
? Local vars[/code]
[code python]forms.ModelChoiceField(queryset=blog_cat.objects.all(), ....)[/code]
Офлайн
все до боли просто….
'blogs_cat': forms.ModelChoiceField(queryset=blog_cat.objects.all(), empty_label=None, to_field_name="blog_cat_name")
[code]class BlogForm(forms.ModelForm): class Meta(): model = blogs blogs_cat = forms.ModelChoiceField(queryset=blog_cat.objects.all(), empty_label=None, to_field_name="blog_cat_name") fields = ['blogs_title', 'blogs_cat', 'blogs_text'] widgets = { 'blogs_title': forms.TextInput(attrs={'placeholder': 'Название', 'max_length': '313'}), 'blogs_text': forms.Textarea(attrs={'placeholder': 'Текст', 'cols': 50, 'rows': 15}), } [/code]
Отредактировано Xa_xoo (Сен. 6, 2016 10:56:33)
Офлайн