Django URLField and HTML5? - django

Is it possible to make django's (v1.2) URLField output an HTML5 input tag where type="url"?
------------- SOLUTION -------------
from django.forms import ModelForm
from django.forms import widgets
from django.forms import fields
from models import MyObj
class URLInput(widgets.Input):
input_type = 'url'
class MyObjForm(ModelForm):
url = fields.URLField(widget=URLInput())
class Meta:
model = MyObj

You have to create a custom widget for that.
class URLInput(forms.TextInput):
input_type = 'url'
Then you can pass this widget to URLField constructor:
class MyForm(forms.Form):
url = forms.URLField(widget=URLInput())

Related

Django CheckboxSelectMultiple not displaying in ModelForm

I'm trying to use CheckboxSelectMultiple in a form, instead of the SelectMultiple default. I tried two different ways to make it display as checkboxes, but they both display the default SelectMultiple on my local webpage. Everything else renders correctly ( {{ form.as_table }} ). Would someone point me in the right direction? Thanks,
Django version 3.0.2
Python 3.6.9
models.py
class MyModel(models.Model):
m2m = models.ManyToManyField('OtherModel', blank=True)
[...]
forms.py
from django.forms import ModelForm
from myapp.models import MyModel
class MyModelModelForm(ModelForm):
[...]
# I tried this...
m2m = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple)
class Meta:
model = MyModel
fields = '__all__'
# or this...
widgets = {
'm2m': forms.CheckboxSelectMultiple,
}
views.py
from django.views.generic.edit import CreateView
from myapp.models import MyModel
class MyModelCreate(CreateView):
model = MyModel
fields = '__all__'
template_name = 'MyApp/mymodel_form.html'
You did not use the form you constructed. You simply constructed a ModelForm, and then let the CreateView create another ModelForm since you did not specify a form_class [Django-doc].
You can thus update the CreateView, and work with:
from django.views.generic.edit import CreateView
from myapp.models import MyModel
from myapp.forms import MyModelModelForm
class MyModelCreate(CreateView):
model = MyModel
form_class = MyModelModelForm
template_name = 'MyApp/mymodel_form.html'

How can we append extra fields dynamically in serializer.py using request object

http://127.0.0.1:8000/?words=anger
and I want to add 'anger' to the field serializer dynamically.
from .models import Synonym
from rest_framework import serializers
class SynonymSerializer(serializers.ModelSerializer):
class Meta:
model = Synonym
fields = ('word',)

DateField doesn't work in forms.py

I'm trying to create a date input field, but it shows as a simple input text, i'm using django 1.10, thanks in advance.
from django import forms
import datetime
from .models import Student
class StudentForm(forms.ModelForm):
class Meta:
model = Student
fields = ["name","date"]
widgets = {
'date': forms.DateInput(format="%d/%m/%Y")}

Django autocomplete light, taggit and the admin interface

I'm trying to use autocomplete_light and taggit both on an admin form.
I've read the docs on integrating autocomplete light and taggit here,
and the docs on integrating autocomplete light in the admin here. But there seems to be little (or no) discussion on doing both at the same time.
what I've got so far.
In models.py:
from django.db import models
from taggit.managers import TaggableManager
from taggit.models import TagBase, GenericTaggedItemBase
class MyTag(TagBase):
description = models.CharField(max_length = 250, blank = True, null = True)
class MyTagThroughModel(GenericTaggedItemBase):
tag = models.ForeignKey(MyTag, related_name = "tagged_items")
class MyModel(models.Model):
Name = models.CharField(max_length = 200)
...
tags = TaggableManager(through = MyTagThroughModel)
In autocomplete_light_registry.py:
import autocomplete_light
from models import MyTag
autocomplete_light.register(MyTag)
How am I meant to structure admin.py?
If this was a non-admin form, the field would be given as:
tags = TagField(widget = TagWidget('MyTagAutocomplete'))
If this was a non-taggit admin form, I would add the following to the admin model class:
form = autocomplete_light.modelform_factory(MyTag)
How can I combine the two?
How am I meant to structure admin.py?
Here's an example to autocomplete Tags. It shows you how autocomplete_light and taggit work on admin and non-admin forms.
models.py
from django.db import models
from taggit.managers import TaggableManager
class MyModel(models.Model):
name = models.CharField(max_length = 200)
tags = TaggableManager(blank=True)
autocomplete_light_registry.py
import autocomplete_light
from taggit.models import Tag
autocomplete_light.register(Tag)
forms.py
from django import forms
import autocomplete_light
from autocomplete_light.contrib import taggit_tagfield
from models import MyModel
class MyModelForm(forms.ModelForm):
tags = taggit_tagfield.TagField(widget=taggit_tagfield.TagWidget('TagAutocomplete'))
class Meta:
model = MyModel
widgets = {
'tags': autocomplete_light.TextWidget('TagAutocomplete'),
}
admin.py
from django.contrib import admin
import autocomplete_light
from models import MyModel
from forms import MyModelForm
class MyModelAdmin(admin.ModelAdmin):
form = MyModelForm
model = MyModel
admin.site.register(MyModel, MyModelAdmin)
views.py
from django.views.generic.edit import CreateView
from models import MyModel
from forms import MyModelForm
class CreateMyModel(CreateView):
model = MyModel
form_class = MyModelForm
urls.py
from django.conf.urls import patterns, url
from views import CreateMyModel
urlpatterns = patterns('',
url(r'^create/$', CreateMyModel.as_view()),
)
The quick docs seem to be more straightforward to understand than the docs you were looking at.
Consider using django-taggit-autosuggest instead.
It works best with the django-grapelli admin skin.

Removing fields from django-rest-framework view

is there any way to show only a list of fields or excluding some of them when using django-rest-framework?
Here's my app/views.py:
from rest_framework.generics import ListAPIView
from .models import PhpbbUsers
class UsersReadView(ListAPIView):
model = PhpbbUsers
Obiously there are some user information that I don't want to show to everyone. How could I do?
Solution code
from rest_framework import generics, serializers
from .models import PhpbbUsers
class UsersSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = PhpbbUsers
fields = ('user_id', 'username', 'user_avatar')
class UsersReadView(generics.ListAPIView):
model = PhpbbUsers
serializer_class = UsersSerializer
Set the serializer_class attribute on the view.
See the quickstart for a good example: http://django-rest-framework.org/tutorial/quickstart.html