Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does Django make Python look ugly?
    primarykey
    data
    text
    <p>I took up the Python programming language because of its design philosophies, its great community and most importantly for me its beautiful syntax. However, recently I've been a left a little disheartend. In my attempts to customise Django I've come across code that I think syntactically could be cleaner. I'm by no means a seasoned Python programmer, in-fact I've only been using it properly for the past few months. I'd appreciate your insights and your views. </p> <p>Here are some examples of code I have come across:</p> <p>Why is there a need for the slash?</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>Could this be written more elegantly?</p> <pre><code> rel_name = other_model._meta.pk.name self.lookup_kwarg = '%s__%s__exact' % (self.field_path, rel_name) self.lookup_kwarg_isnull = '%s__isnull' % (self.field_path) self.lookup_val = request.GET.get(self.lookup_kwarg, None) self.lookup_val_isnull = request.GET.get( self.lookup_kwarg_isnull, None) self.lookup_choices = f.get_choices(include_blank=False) </code></pre> <p>One thing I don't understand is why the code after the if statement for each and statement is on seperate lines?</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: extra = 1 else: extra = 0 return len(self.lookup_choices) + extra &gt; 1 </code></pre> <p>This just looks messy!</p> <pre><code>def choices(self, cl): from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE yield {'selected': self.lookup_val is None and not self.lookup_val_isnull, 'query_string': cl.get_query_string( {}, [self.lookup_kwarg, self.lookup_kwarg_isnull]), 'display': _('All')} for pk_val, val in self.lookup_choices: yield {'selected': self.lookup_val == smart_unicode(pk_val), 'query_string': cl.get_query_string( {self.lookup_kwarg: pk_val}, [self.lookup_kwarg_isnull]), 'display': val} if isinstance(self.field, models.related.RelatedObject) \ and self.field.field.null or hasattr(self.field, 'rel') \ and self.field.null: yield {'selected': bool(self.lookup_val_isnull), 'query_string': cl.get_query_string( {self.lookup_kwarg_isnull: 'True'}, [self.lookup_kwarg]), 'display': EMPTY_CHANGELIST_VALUE} </code></pre> <p>Please don't get me wrong, I'm not doing a disservice to the many contributors of Django, on the contrary I really admire them and I'm grateful. I appreciate that maybe it's my lack of experience with Python itself or that bits of code that make the syntax look unclean are in-fact core features of the Python programming language.</p> <p><strong>Just to make it clear this question is genuine and sincere, I ask this question in the spirit of learning and discussion. If you don't have anything conducive to contribute please don't respond.</strong></p> <p>Thank You</p>
    singulars
    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.
 

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