source code

  • forms.Widget forms/
  • forms.Field forms/
  • ModelForm forms/ class ModelForm(six.with_metaclass(ModelFormMetaclass, BaseModelForm)):
  • Model db/models/ class Model(six.with_metaclass(ModelBase)):

app namespace and instance name space


generic explaining.
in admin:

     include('', namespace='foo', app_name='app_poll')),

app_name : means an django project can have multiple applications. this is application namespace.
namespace: means an app can have multiple instances deployed. for example, you have a photo sharing system, you can deploy it for different customers, one for car, the other for fruit. this is call instance namepsaces

django.core.urlresolvers.resolve can be used:

r = resolve(request.path)
r.app_name  # the app name
r.namespace # the the currently active instance

An example of instance namespace:

how to use it.

when we refer it, in template:
<li><a href="{% url 'foo:detail' %}">{{ poll.question }}</a></li>
dynamic generating

in above we still have to hard code foo:detail. in fact, it is possible to dynamic generating it as:

class AlbumCreateView(TemplateView):
    template_name = 'path/to/my/template.html'
    def render_to_response(self, context, **response_kwargs):
        response_kwargs['current_app'] = resolve(self.request.path).namespace
        return super(AlbumPageView, self).render_to_response(context, **response_kwargs)

Another approach is add it as argument in url

for reverse:
HttpResponseRedirect(reverse('foo:detail', args=(id,)))

note, if you use 'app_poll:detail', django will resolve to the current instance in template. but in reverse(), you have to supply current_app parameter to identify the
right instance, such as:

reverse('app_poll:details', current_app=resolve(self.request.path).namespace))

if app_name is not defined in, then you can write it like:

reverse('details', current_app=resolve(self.request.path).namespace))

customize field and widget

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License