Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>1) You need slash, because usually, but not always, Django adheres to pep8, where lines should have at most 80 characters. The better way to write this though, is:</p> <pre><code>from django.contrib.admin.util import (get_model_from_relation, reverse_field_path, get_limit_choices_to_from_path) </code></pre> <p><code>\</code> should generally be avoided.</p> <p>2) There is nothing inelegant with this code. Simply attributes required for making lookup are created. Why do you feel it's not elegant? How would you prefer this to be written?</p> <p>3) Again to satisfy the need of having line shorter than 80 characters. This could be rewritten using <code>()</code> and made shorter:</p> <pre><code>def has_output(self): extra = (isinstance(self.field, models.related.RelatedObject) and self.field.field.null or hasattr(self.field, 'rel') and self.field.null) extra = 1 if extra else 0 return len(self.lookup_choices) + extra &gt; 1 </code></pre> <p>However since Django uses Python 2.4 (I think they are bumping version soon or already did it), they can't use inline <code>if-else</code>.</p> <p>On the other hand it could be also written in shorter way:</p> <pre><code>def has_output(self): if isinstance(self.field, models.related.RelatedObject) \ and self.field.field.null or hasattr(self.field, 'rel') \ and self.field.null: return len(self.lookup_choices) &gt; 0 else: return len(self.lookup_choices) &gt; 1 </code></pre> <p>But I feel the original way is slightly clearer by having the <code>extra</code> variable. Here you would need a comment, why it's either 0 or 1. With extra you don't need a comment and it's perfectly clear. I dislike comments, so I prefer the first way :-)</p> <p>4) This indeed looks messy. I believe it would be better to split it into three smaller methods, each potentially yielding someting. But then I am not sure that it's allowed in python2.4 (or python2.5) to yield from subroutine (I have some vague memory, that this was introduced later or even in py3). Anyway I would put creation of those dictionaries into a separate methods, because it seems very untrivial. What I would prefer is this:</p> <pre><code>def choices(self, cl): from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE yield self._some_default_choice() for pk_val, val in self.lookup_choices: yield self._choice_from_lookup_choices(pk_val, val) if isinstance(self.field, models.related.RelatedObject) \ and self.field.field.null or hasattr(self.field, 'rel') \ and self.field.null: yield self._some_conditional_choice() </code></pre> <p>Of course I would use some more meaningful names for submethods, but I don't see the full context and I don't really know what those choices are.</p> <p><strong>Finally:</strong></p> <p>What you see here is Python2 pushed to its limits. Django is a big framework. There are some <em>features</em> that are simply result of Django being large project, that has been now developed for several years and people learning new stuff. Fortunately Django developers are slowly removing stuff they believe is wrong, e.g. changing default project structure in Django 1.4, deprecating stuff and bumping python version. You can actually learn a lot from reading django code and asking questions. You probably can learn even more by trying to refactor some code and then learning, why it's not that easy and why it must be left the way it is ;-) Try it, it will be fun :-)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload