Re: m2m with intermediary - model design? Disappears from Django-Admin?
Gday Victor, Glad you found the inline admin method for attaching your m2m's with throughs. It isn't possible to to use the filter_horizontal widget for through m2m, because, as you noted, there's no space for the extra fields to be input. You could always try to extend the widget, but this is more trouble than it's worth, and I haven't seen it done acceptably. If you don't like all the space taken up by the inline, try making them collapsible with js, maybe (you can define js in the ModelAdmin inner class Media: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-media-definitions). As for the name "FirmRating object", define a unicode method on your class, eg: def __unicode__(self): return "%s - %s: %s) % (self.article, self.firm, self.rating) Cheers, Brenton On Jun 25, 10:24 am, Victor Hooi wrote: > heya, > > Ok, scratch all that, I'm an idiot. > > A simple restart of my Apache process, and the m2m intermediary inline > works *grins*. > > Thanks anyhow to the awesome Django community for an awesome tool =). > > I do have a final question though. I can now edit "Firms" as inlines > from the "Article" page, which is good. > > However, to be honest, the filter_horizontal widget was a much better > interface. I suppose there's no way to somehow use the > filter_horizontal widget within the inline, and then tack on a > "Rating" field as part of the inline? > > Also, I notice that in my inline, it says "FirmRating object" right > above the select box:' > > http://twitpic.com/1zo4im/full > > Any way to hide/tweak that text? > > Cheers, > Victor > > On Jun 25, 10:09 am, Victor Hooi wrote: > > > > > heya, > > > Also, I should add I did try using the inlines as described in the > > docs > > >http://docs.djangoproject.com/en/dev/ref/contrib/admin/#working-with-... > > > In my admin.py, I've imported "FirmRating" at the top. > > > I've then created an inline for it: > > > class FirmRatingInline(admin.TabularInline): > > model = FirmRating > > extra = 1 > > > then used this in ArticleAdmin: > > > class ArticleAdmin(admin.ModelAdmin): > > #inlines = [ > > # CategoryInline, > > #] > > date_hierarchy = 'publication_date' > > filter_horizontal = ('firm', 'spokesperson') > > list_display = ('title', 'publication_date', 'entry_date', > > 'category', 'subject', 'source_publication', 'weekly_summary') > > list_editable = ('publication_date', 'category', 'subject', > > 'source_publication') > > list_filter = ('publication_date', 'entry_date', 'category', > > 'subject', 'source_publication') > > search_fields = ('title', 'publication_date', 'abstract', > > 'category__name', 'subject__name', 'source_publication__name', > > 'page_number', 'url') > > > However, there's still no visible widget for "FirmRating" on the "Add > > Article" page. > > > I can't tell just from the docs, but is this inline only for use on > > the main Article list page, but not on the Add Article page? Or is the > > above admin code meant to make the widget visible on the "Add Article" > > page as well? > > > Cheers, > > Victor > > > On Jun 25, 10:01 am, Victor Hooi wrote: > > > > heya, > > > > NB: This is a followup to this: > > > >http://groups.google.com/group/django-users/browse_thread/thread/0fdc... > > > > but I thought I'd also ask about the model design. > > > > To provide some background, we have a Django app that contains a list > > > of journal articles. > > > > Each "Article" also has a m2m relationship to "Firm" as well as > > > "Spokesperson" > > > > class Article(models.Model): > > > title = models.CharField(max_length=100) > > > publication_date = models.DateField() > > > abstract = models.TextField() # Can we restrict this to 450 > > > characters? > > > ...fields ommited for brevity... > > > firm = models.ManyToManyField(Firm, null=True, blank=True, > > > through='FirmRating') > > > spokesperson = models.ManyToManyField(Spokeperson, null=True, > > > blank=True, through='SpokespersonRating') > > > > The intermediary models, FirmRating and SpokespersonRating look like > > > this: > > > > class FirmRating(models.Model): > > > firm = models.ForeignKey(Firm) > > > article = models.ForeignKey(Article) > > > rating = models.IntegerField() > > > > Basically, we use it to store the m2m link, as well as a "rating" > > > assigned to each relationship. > > > > The issue is, as soon as I change it from a normal m2m relationship to > > > one with a "through" attribute, it seems to completely disappear in > > > the Django Admin from the "Add Article" page. Before, I had a nice > > > little filter_horizontal widget to create the relationships. No, > > > zippo...that's not a bug, is it? > > > > Anyhow, Firstly, am I doing this the right way? Or is there another > > > way to create an Article, have it m2m with Firms/Spokesperson, and > > > assign an individual rating to each relationship? > > > > Secondly, is there some way of making a m2m with int
process_response not getting called in custom middleware.
Gday folks, I've written a middleware to add some session variables based on the request variable; this is done in process_request, which always returns None. I'd like to set some cookies as well, based on the same session variables, which should be done in process_response. I wrote the method, but found it was not dropping cookies, so I simplified it some, here is my current version: def process_response(self, request, response): """Drop cookies based on whether a user has selected an offer Always drop (refresh) the greencode cookie and RefCode cookie Drop offer cookies based on whether the user has clicked- through on an offer. """ print "BAH!" #if request.session.get('grc'): #response.set_cookie('_gc', request.session['grc'], 31536, #domain=domain) #response.set_cookie('RefCode', request.session['refcode'], 31536, #domain=domain) return response This, unfortunately, doesn't print anything to the server log (either when running the development server, or on an apache/mod_wsgi environment). Even when I change response to an empty HttpResponse(), I still get the response the view returned. So surely this all means that the process_response method is not being called. Does anyone have any suggestion as to why this might be happening, and how to rectify it? Thanks, Brenton -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Admin Media output order?
Gday, Overriding the admin templates is the only way to do this: http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#overriding-admin-templates You can either try listing the items how you want (this will mean a change to the templates everytime you register something new), or doing it using JS (you'll prolly have to change this whenever you register something new as well). I generally opt to set verbose_names on my models, so that they get listed into groups, eg: Offer Banners Offer Sidebar Banners Referers - Domain Referers Referers - Search Engine Referers Referers - URL Referers It's the cleanest way I've found to do such a thing. Cheers, Brenton On Jun 25, 5:18 am, ringemup wrote: > Is there any way to change the order in which admin media are output > (e.g. outputting a ModelAdmin-level javascript after a widget-level > script)? > > Thanks -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: m2m with intermediary - model design? Disappears from Django-Admin?
Gday again Victor, If you think about it, the filter_horizontal widget really doesn't make sense in this context, because selecting multiple objects to link to will still require a list of them somewhere (as you mention) to set the rating. Since you like the searchability, perhaps adding an autocomplete to the inline admins would be helpful? I'm not a huge UI guy though, so maybe you're just more imaginative =D If you come up with anything, let me know. In terms of resources, all I can recommend is reading through the source. Sorry about that! Best of luck, Brenton. On Jun 25, 12:41 pm, Victor Hooi wrote: > heya, > > Brenton: Thanks for the reply =). > > Hmm, I was hoping to use the filter_horizontal widget to set the > relationship, and just have a box below that for setting the rating. > But you're right, we'd need a table for each rating for each > relationship, and there's a room issue. > > I might look into extending the widget - is there any good > documentation on extending Django admin widgets? I saw this article: > > http://www.fictitiousnonsense.com/archives/22 > > but I'm not sure if that's still up-to-date, and I was hoping for more > details/examples to learn how to do it well. Any good reading > recommendations on this topic? > > Also, it's not so much an issue with the inlines taking up space, as > that the filter_horizontal widget, with the in-built search box, the > "choose all" and "clear all" links is just a good UI, and looks quite > nice, to boot. I'd really like to integrate it in somehow to the > inline if possible...lol. > > Anyhow, finally, thanks for the tip about __self__, completely didn't > think about that. Will set that now. > > Cheers, > Victor > > On Jun 25, 11:03 am, iliveinapark > wrote: > > > > > Gday Victor, > > > Glad you found the inline admin method for attaching your m2m's with > > throughs. It isn't possible to to use the filter_horizontal widget for > > through m2m, because, as you noted, there's no space for the extra > > fields to be input. You could always try to extend the widget, but > > this is more trouble than it's worth, and I haven't seen it done > > acceptably. If you don't like all the space taken up by the inline, > > try making them collapsible with js, maybe (you can define js in the > > ModelAdmin inner class > > Media:http://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-me...). > > > As for the name "FirmRating object", define a unicode method on your > > class, eg: > > > def __unicode__(self): > > return "%s - %s: %s) % (self.article, self.firm, self.rating) > > > Cheers, > > Brenton > > > On Jun 25, 10:24 am, Victor Hooi wrote: > > > > heya, > > > > Ok, scratch all that, I'm an idiot. > > > > A simple restart of my Apache process, and the m2m intermediary inline > > > works *grins*. > > > > Thanks anyhow to the awesome Django community for an awesome tool =). > > > > I do have a final question though. I can now edit "Firms" as inlines > > > from the "Article" page, which is good. > > > > However, to be honest, the filter_horizontal widget was a much better > > > interface. I suppose there's no way to somehow use the > > > filter_horizontal widget within the inline, and then tack on a > > > "Rating" field as part of the inline? > > > > Also, I notice that in my inline, it says "FirmRating object" right > > > above the select box:' > > > >http://twitpic.com/1zo4im/full > > > > Any way to hide/tweak that text? > > > > Cheers, > > > Victor > > > > On Jun 25, 10:09 am, Victor Hooi wrote: > > > > > heya, > > > > > Also, I should add I did try using the inlines as described in the > > > > docs > > > > >http://docs.djangoproject.com/en/dev/ref/contrib/admin/#working-with-... > > > > > In my admin.py, I've imported "FirmRating" at the top. > > > > > I've then created an inline for it: > > > > > class FirmRatingInline(admin.TabularInline): > > > > model = FirmRating > > > > extra = 1 > > > > > then used this in ArticleAdmin: > > > > > class ArticleAdmin(admin.ModelAdmin): > > > > #inlines = [ > > > > # CategoryInline, > > > > #] > > > > date_hierarchy = 'publication_date' > > >
Re: Admin Media output order?
Ha, I think I missed the point of that question... See the admin templates part of my answer, ignore the rest - I don't think it applies to your question, but it's a nifty trick anyways. Brenton. On Jun 25, 11:37 am, iliveinapark wrote: > Gday, > > Overriding the admin templates is the only way to do > this:http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#overriding-ad... > > You can either try listing the items how you want (this will mean a > change to the templates everytime you register something new), or > doing it using JS (you'll prolly have to change this whenever you > register something new as well). > > I generally opt to set verbose_names on my models, so that they get > listed into groups, eg: > > Offer Banners > Offer Sidebar Banners > Referers - Domain Referers > Referers - Search Engine Referers > Referers - URL Referers > > It's the cleanest way I've found to do such a thing. > > Cheers, > Brenton > > On Jun 25, 5:18 am, ringemup wrote: > > > > > Is there any way to change the order in which admin media are output > > (e.g. outputting a ModelAdmin-level javascript after a widget-level > > script)? > > > Thanks -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Making InlineModelAdmin objects required.
Hi all, I'm wondering if there's a way to make InlineModelAdmin objects required, so that if a user saves on an admin page without completing the fields of the inline model, it displays big red "This field is required." Errors like it does with the included fields. Cheers. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Making InlineModelAdmin objects required.
Thanks mate, I'll look into that at some point. It's not a major requirement of the project, so I'll put it on the backburner for now. Cheers, Brenton. On Jan 21, 5:21 pm, "mattimust...@gmail.com" wrote: > Hi, > > Take a look athttp://code.google.com/p/wadofstuff/wiki/WadOfStuffDjangoForms > as the RequireOneFormSet class may give you some hints on how to do > this. > > regards > > Matthew > > On Jan 21, 3:57 pm, iliveinapark > wrote: > > > > > Hi all, > > > I'm wondering if there's a way to make InlineModelAdmin objects > > required, so that if a user saves on an admin page without completing > > the fields of the inline model, it displays big red "This field is > > required." Errors like it does with the included fields. > > > Cheers. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
ManyToManyField through relation between a model and a subclass of it.
Hiya folks, I'm having a problem defining a relationship between a model, and another model that subclasses the first. ie: class Page(models.Model): ... sections = models.ManyToManyField('Section', through='PageSection', related_name='pages', symmetrical=False, blank=True, null=True,) class PageSection(models.Model): """An affiliation of a page with a section""" page = models.ForeignKey('Page', related_name='page',) section = models.ForeignKey('Page', related_name='section',) weight = models.IntegerField(default=0,) class Section(Page): ... class Category(Section): ... These models are defined in separate files, but have the app_label='pages' option set in the Meta class (except for Category, which is in a different app, but the relevant models of pages are pulled in), so it syncdb's without issue. I have a StackedInline ModelAdmin set up for PageSection, and it is in the inlines list for Category's ModelAdmin. Now, as I said, this syncdb's just fine, and the server runs, however when I go to create a Category, I get: has more than 1 ForeignKey to Which I believe relates to this, from the docs: "There are a few restrictions on the intermediate model: Your intermediate model must contain one - and only one - foreign key to the target model (this would be Person in our example). If you have more than one foreign key, a validation error will be raised. Your intermediate model must contain one - and only one - foreign key to the source model (this would be Group in our example). If you have more than one foreign key, a validation error will be raised. The only exception to this is a model which has a many-to-many relationship to itself, through an intermediary model. In this case, two foreign keys to the same model are permitted, but they will be treated as the two (different) sides of the many-to-many relation. When defining a many-to-many relationship from a model to itself, using an intermediary model, you must use symmetrical=False (see the model field reference)." The thing is, the intermediary model has FK's to different classes (though one is a subclass of the other), and the error I'm getting says that it is referencing the Category class twice, which it is - it's referencing two different ancestors of it. I know this seems unnecessarily complex, but this is how my project has to be structured. Does anyone have a work-around that will let me do what I want to? Cheers. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: having an ManyToMany field, and have it optional
Both are required for a ForeignKey or ManyToManyField to be empty, ie: class GoalTag(models.Model): goals = models.ManyToManyField(Goal, blank=True, null=True,) On Feb 11, 5:20 pm, Atamert Ölçgen wrote: > On Thursday 11 February 2010 05:18:44 kamilski81 wrote:> Is it possible to do: > > > class GoalTag(models.Model): > > goals = models.ManyToManyField(Goal, null=True) > > Try blank=True. That null=True is possibly redundant there. > > -- > Saygılarımla, > Atamert Ölçgen > > -+- > --+ > +++ > > www.muhuk.com > mu...@jabber.org -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Make a new model outside of models.py
In the model's meta class, give it a field app_label, eg: class MyModel(models.Model): ... class Meta: app_label = "myapp" I use this to split my models into multiple files in a models subdirectory of my app. Adding a __init__.py with from modelfile import * for each model file makes this directory into a models submodule. On Feb 12, 2:49 am, Bill Freeman wrote: > One issue is that for syncdb to work it has to find all of the models. > It does this, > IIUC, by importing models from each INSTALLED_APP. If you can arrange for > that to import your added model, you are on the right track. For example, you > can make your models.py files each import an extra file containing the extra > model(s) *for that app*. But I suspect that you are trying to do this without > modifying the models.py files of the apps, and I don't see a simple approach, > and complicated approaches (such as iterating through INSTALLED_APPS > yourself and plugging an extra class into each in django's internal > structures) > is likely to be hard to maintain, and be dependent on django internals that > can > change from version to version. > > Bill > > > > On Thu, Feb 11, 2010 at 3:51 AM, Sameer Rahmani wrote: > > Hi , > > is there any way to build a new model from outside of models.py ? or in > > other application ? > > > i want to build a model for a class for each application > > > -- > > lxsameer > > > -- > > You received this message because you are subscribed to the Google Groups > > "Django users" group. > > To post to this group, send email to django-us...@googlegroups.com. > > To unsubscribe from this group, send email to > > django-users+unsubscr...@googlegroups.com. > > For more options, visit this group at > >http://groups.google.com/group/django-users?hl=en. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: having an ManyToMany field, and have it optional
I guess you're correct Atamert, though I'm sure this is still the case for ForeignKey fields, isn't it? On Feb 11, 8:16 pm, Atamert Ölçgen wrote: > On Thursday 11 February 2010 08:26:10 iliveinapark wrote:> Both are required > for a ForeignKey or ManyToManyField to be empty, ie: > > > class GoalTag(models.Model): > > goals = models.ManyToManyField(Goal, blank=True, null=True,) > > That is not true. Check contrib apps for examples. > > -- > Saygılarımla, > Atamert Ölçgen > > -+- > --+ > +++ > > www.muhuk.com > mu...@jabber.org -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Object data change approvals.
Gday all, I'm trying to find a way to have changes made to a model object require approval before becoming the live data on my site. I had thought about cloning the object, and having the clone stored as a draft, but making it live would require either swapping all the data from the draft to the original object, or changing all references to the original to reference the draft instead. Does anyone have any ideas on a nice way to do this? Thanks. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Object data change approvals.
Thanks very much, Bruno, this is a pretty simple way of doing what I want. Now I just have to figure out to apply it generically over multiple levels of inheritance =/ Cheers mate. On Feb 12, 10:00 pm, bruno desthuilliers wrote: > On Feb 12, 11:24 am, bruno desthuilliers wrote: > > (snip) > > oops, forgot to actually use the custom manager: > > > class MyModelRevision(models.Model): > > (snip) > objects = MyModelRevisionManager() > > > > > class Meta: > > unique_together = (('MyModel', 'published'),) -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Query to collect objects in a hierarchical model structure.
Hello all, So I'm making a sort of FAQ, with a hierarchical category structure: 28 class SupportCategory(models.Model): 29 title = models.CharField(max_length=100) 30 slug = models.CharField(max_length=100) 31 parent = models.ForeignKey('self', blank=True, null=True, 32 related_name='children') And Questions can belong to many categories: 55 class Question(models.Model): 56 timestamp = models.DateTimeField(auto_now=True) 57 question = models.TextField() 58 answer = models.TextField() 59 categories = models.ManyToManyField(SupportCategory, 60 related_name='questions') 61 keywords = models.ManyToManyField(Keyword, 62 related_name='questions') 63 attachments = models.ManyToManyField(Attachment, blank=True, 64 related_name='questions') 65 related_questions = models.ManyToManyField('self', blank=True) Obviously, for a given category, cat.questions.all() will return all questions assigned to cat. How can I write a query (or even better, a manager) which will return all questions belonging to cat and all of it's ancestors (cat.children, and their children, etc). Thanks in advance for any suggestions. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.