Esiste un riferimento Django?

8

Sono nuovo di Django. Ho trovato le guide tematiche ufficiali link un luogo eccellente per imparare da. Ma ora sono in un posto in cui ho bisogno di un rapido riferimento per vedere quali metodi ha un oggetto, quali argomenti richiede, cosa restituisce ecc., Un po 'come i documenti Python o i documenti Java. Più di un riferimento di un tutorial. I documenti ufficiali sono in stile tutorial e non hanno necessariamente riferimenti a tutti i metodi.

Mi sto perdendo qualcosa qui? I documenti ufficiali di Django sono sufficienti per tutti?

    
posta pcx 20.10.2011 - 13:31
fonte

3 risposte

6

Potrei non essere corretto, ma ho usato django da oltre un anno e non ho mai trovato una guida del genere.

Un po 'sfortunato - mi piacerebbe avere una funzione di riferimento come hai detto tu. Tuttavia, come risorsa aggiuntiva il link è molto buono. Ho sempre pensato di costruirne uno, ma sono molto pigro.

C'è anche un altro riferimento che puoi usare quando sei bloccato link

    
risposta data 20.10.2011 - 13:37
fonte
7

Python ha un meccanismo integrato per questo: docstrings. Esempio:

>>> import django.forms
>>> help(django.forms.ModelForm)
Help on class ModelForm in module django.forms.models:

class ModelForm(BaseModelForm)
 |  Method resolution order:
 |      ModelForm
 |      BaseModelForm
 |      django.forms.forms.BaseForm
 |      django.utils.encoding.StrAndUnicode
 |      __builtin__.object
 |  
 |  Data and other attributes defined here:
 |  
 |  __metaclass__ = <class 'django.forms.models.ModelFormMetaclass'>
 |  
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from BaseModelForm:
 |  
 |  __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, initial=None, error_class=<class 'django.forms.util.ErrorList'>, label_suffix=':', empty_permitted=False, instance=None)
 |  
 |  clean(self)

 |  
 |  save(self, commit=True)
 |      Saves this ''form'''s cleaned_data into model instance
 |      ''self.instance''.
 |      
 |      If commit=True, then the changes to ''instance'' will be saved to the
 |      database. Returns ''instance''.
 |  
 |  validate_unique(self)
 |      Calls the instance's validate_unique() method and updates the form's
 |      validation errors if any were raised.
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from django.forms.forms.BaseForm:
 |  
 |  __getitem__(self, name)
 |      Returns a BoundField with the given name.
 |  
 |  __iter__(self)
 |  
 |  __unicode__(self)
 |  
 |  add_initial_prefix(self, field_name)

 |      Add a 'initial' prefix for checking dynamic initial values
 |  
 |  add_prefix(self, field_name)
 |      Returns the field name with a prefix appended, if this Form has a
 |      prefix set.
 |      
 |      Subclasses may wish to override.
 |  
 |  as_p(self)
 |      Returns this form rendered as HTML <p>s.
 |  
 |  as_table(self)
 |      Returns this form rendered as HTML <tr>s -- excluding the <table></table>.
 |  
 |  as_ul(self)
 |      Returns this form rendered as HTML <li>s -- excluding the <ul></ul>.
 |  
 |  full_clean(self)
 |      Cleans all of self.data and populates self._errors and
 |      self.cleaned_data.
 |  
 |  has_changed(self)
 |      Returns True if data differs from initial.
 |  
 |  hidden_fields(self)
 |      Returns a list of all the BoundField objects that are hidden fields.
 |      Useful for manual form layout in templates.
 |  
 |  is_multipart(self)
 |      Returns True if the form needs to be multipart-encrypted, i.e. it has
 |      FileInput. Otherwise, False.
 |  
 |  is_valid(self)
 |      Returns True if the form has no errors. Otherwise, False. If errors are
 |      being ignored, returns False.
 |  
 |  non_field_errors(self)
 |      Returns an ErrorList of errors that aren't associated with a particular
 |      field -- i.e., from Form.clean(). Returns an empty ErrorList if there
 |      are none.
 |  
 |  visible_fields(self)
 |      Returns a list of BoundField objects that aren't hidden fields.
 |      The opposite of the hidden_fields() method.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from django.forms.forms.BaseForm:
 |  
 |  changed_data
 |  
 |  errors
 |      Returns an ErrorDict for the data provided for the form
 |  
 |  media
 |      Provide a description of all media required to render the widgets on this form
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from django.utils.encoding.StrAndUnicode:
 |  
 |  __str__(self)
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from django.utils.encoding.StrAndUnicode:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)

Quindi puoi cercare le docstring dei singoli metodi:

>>> help(django.forms.ModelForm.is_valid)
Help on method is_valid in module django.forms.forms:

is_valid(self) unbound django.forms.models.ModelForm method
    Returns True if the form has no errors. Otherwise, False. If errors are
    being ignored, returns False.
    
risposta data 20.10.2011 - 13:44
fonte
1

Personalmente, trovo che usare pycharm IDE aiuta a velocizzare significativamente lo sviluppo di django. Anche nella shell django puoi usare la funzione ipython per ottenere docstring e codice sorgente, digitando qualcosa e seguito da? (informazioni di base) o ?? (codice sorgente). Molto utili sono anche altre funzionalità di ipython come il completamento delle schede (solo un livello).

In [1]: from django.contrib.auth.models import User

In [2]: User ?
Type:       ModelBase
Base Class: <class 'django.db.models.base.ModelBase'>
String Form:    <class 'django.contrib.auth.models.User'>
Namespace:  Interactive
File:       /usr/local/lib/python2.6/dist-packages/Django-1.2.5-py2.6.egg/django/contrib/auth/models.py
Docstring:
    Users within the Django authentication system are represented by this model.

    Username and password are required. Other fields are optional.

Constructor information:
Definition: User(self, *args, **kwargs)


In [3]: User ??
Type:             ModelBase
Base Class:       <class 'django.db.models.base.ModelBase'>
String Form:   <class 'django.contrib.auth.models.User'>
Namespace:        Interactive
File:             /usr/local/lib/python2.6/dist-packages/Django-1.2.5-py2.6.egg/django/contrib/auth/models.py
Source:

class User(models.Model):
    """
    Users within the Django authentication system are represented by this model.

    Username and password are required. Other fields are optional.
    """
    username = models.CharField(_('username'), max_length=30, unique=True, help_text=_("Required. 30 characters or fewer. Letters, numbers and @/./+/-/_ characters"))
    first_name = models.CharField(_('first name'), max_length=30, blank=True)
    last_name = models.CharField(_('last name'), max_length=30, blank=True)
    email = models.EmailField(_('e-mail address'), blank=True)
    password = models.CharField(_('password'), max_length=128, help_text=_("Use '[algo]$[salt]$[hexdigest]' or use the <a href=\"password/\">change password form</a>."))
    is_staff = models.BooleanField(_('staff status'), default=False, help_text=_("Designates whether the user can log into this admin site."))
    is_active = models.BooleanField(_('active'), default=True, help_text=_("Designates whether this user should be treated as active. Unselect this instead of deleting accounts."))
    is_superuser = models.BooleanField(_('superuser status'), default=False, help_text=_("Designates that this user has all permissions without explicitly assigning them."))
    last_login = models.DateTimeField(_('last login'), default=datetime.datetime.now)
    date_joined = models.DateTimeField(_('date joined'), default=datetime.datetime.now)
    groups = models.ManyToManyField(Group, verbose_name=_('groups'), blank=True,
        help_text=_("In addition to the permissions manually assigned, this user will also get all permissions granted to each group he/she is in."))
    user_permissions = models.ManyToManyField(Permission, verbose_name=_('user permissions'), blank=True)
    objects = UserManager()

        class Meta:
        verbose_name = _('user')
[...]
    
risposta data 21.10.2011 - 21:44
fonte

Leggi altre domande sui tag