Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango: How would one organize this big model / manager / design mess?
    text
    copied!<p>To sum things up before I get into bad examples, et al: I'm trying to make an application where I don't have to write code in all my models to limit choices to the current logged in account (I'm not using Auth, or builtin features for the account or login).</p> <p>ie, I <strong>don't</strong> want to have to do something like this:</p> <pre><code>class Ticket(models.Model): account = models.ForeignKey(Account) client = models.ForeignKey(Client) # A client will be owned by one account. content = models.CharField(max_length=255) class TicketForm(forms.ModelForm): class Meta: model = Ticket exclude = ('account',) #First sign of bad design? def __init__(self, *args, **kwargs): super(OrderForm, self).__init__(*args, **kwargs) if self.initial.get('account'): # Here's where it gets ugly IMHO. This seems almost # as bad as hard coding data. It's not DRY either. self.fields['client'].queryset = Client.objects.filter(account=self.initial.get('account')) </code></pre> <p>My idea is to create an <code>Account(models.Model)</code> model with the following custom manager, and subclass it using multi-table inheritance with all of my models. It's giving me a huge brain ache though. Will I still need an <code>account</code> foreign key on each model? Can I access the parent class account for a certain model instance?</p> <pre><code>class TicketManager(models.Manager): def get_query_set(self): return super(TicketManager, self).get_query_set().filter(account=Account.objects.get(id=1)) # Obviously I don't want to hard code the account like this. # I want to do something like this: # return super(ProductManager, self).get_query_set().filter(account=self.account) # Self being the current model that's using this manager # (obviously this is wrong because you're not inside a model # instance , but this is where the confusion comes in for me. # How would I do this?). </code></pre> <p>Please ignore any blaring syntax errors. I typed this whole thing in here.</p> <p>Here's where I got the idea to do this: <a href="http://code.google.com/p/django-namespace/source/browse/#svn/trunk/namespace" rel="nofollow noreferrer">Django Namespace project</a></p>
 

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