Re: Problem when trying to validate a field in a ModelAdmin which has inline forms
Patch has been uploaded to http://code.djangoproject.com/ticket/12780 . After applying the patch you can override def formsets_are_valid(self, formsets, form, form_is_valid, instance, request): and do any complex validation you like. As it's rather arcane, I've provided an example based on your description at http://gist.github.com/296411 . Hope that helps, Mart Sõmermaa > On Jan 15, 8:10 pm, Gabriel Reis wrote: > > > Hey Marcos, > > > In the clean() method of my ClipModelForm class I couldn't manage how to > > reach the ClipDescriptions that are coming from the inline > > ClipDescriptionInline. I think I can't validate there. I spent the whole day > > trying and I couldn't fix. Any help would be very handy! > > > Thanks for your atention. > > > Kind regards, > > > Gabriel > > > Gabriel de Carvalho Nogueira Reis > > Software Developer > > +44 7907 823942 -- 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: Problem when trying to validate a field in a ModelAdmin which has inline forms
See http://code.djangoproject.com/ticket/12780 . I will upload a patch and provide an example, but I can't yet say when. Best, Mart Sõmermaa On Jan 15, 8:10 pm, Gabriel Reis wrote: > Hey Marcos, > > In the clean() method of my ClipModelForm class I couldn't manage how to > reach the ClipDescriptions that are coming from the inline > ClipDescriptionInline. I think I can't validate there. I spent the whole day > trying and I couldn't fix. Any help would be very handy! > > Thanks for your atention. > > Kind regards, > > Gabriel > > Gabriel de Carvalho Nogueira Reis > Software Developer > +44 7907 823942 > > On Fri, Jan 15, 2010 at 4:45 PM, Marco Rogers wrote: > > > I've never had to do this but it sounds you want to work with > > Form.clean() in stead of Form.clean_is_approved(). From the docs: > > > "The Form subclass’s clean() method. This method can perform any > > validation that requires access to multiple fields from the form at > > once." > > > So in your clean method you would grab the submitted inlines and check > > them against the is_approved field on the Clip. I'm not a django > > expert, but that's where I would start. Good luck. > > > :Marco > > > On Jan 14, 6:45 pm, Gabriel Reis wrote: > > > Hi people, > > > > I am having some problem to validate a data in my application. I will try > > to > > > simply the model in here to help you guys to understand the problem. > > > I have 2 models: > > > > class Clip(models.Model): > > > is_approved = models.BooleanField(default=False) > > > language = models.ForeignKey(Language) > > > > class ClipDescription(models.Model): > > > clip = models.ForeignKey(Clip) > > > text = models.TextField() > > > language = models.ForeignKey(Language) > > > > I am editing via ModelAdmin. I defined a ClipModelAdmin class as above > > > (simplified): > > > > class ClipAdmin(admin.ModelAdmin): > > > inlines = [ > > > ClipDescriptionInline > > > ] > > > > So, as you can see, both Clip and ClipDescription are edited in the same > > > page (using the 'inlines' feature). > > > > But the rule is: if the user trigger the 'Save' action, the attribute > > > Clip.is_approved can be True only if there is a ClipDescription instance > > > associated to the Clip instance, having the same language. For example, > > if I > > > have a clip with id=1, language=english and is_approved=True, it can be > > > saved only if there is a clip description with clip_id=1, > > language=english. > > > If not, I want to show the error message 'Cannot approve this clip > > without > > > having a description in English' in the form. > > > > I have already read the official documentation and tried to work with > > > validators, tried to define a ModelForm and its clean_is_approved method, > > > among other workarounds. And I still couldn't make this work. The problem > > is > > > at the clean_is_approved context I couldn't figure out how to get access > > to > > > the form data that is being entered at that moment, to retrieve the Clip > > > descriptions. > > > > I don't if I was clear enough, I can give more details. Any ideas and > > > suggestions will be very appreciated. > > > > Thank you very much for your attention. > > > > Kind regards, > > > > Gabriel > > > > Gabriel de Carvalho Nogueira Reis > > > Software Developer > > > +44 7907 823942 > > > -- > > 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: overriding save() for ManyToManyFields
On Sep 24, 12:52 am, "Daniele Procida" wrote: > Don't I need to run some sort of save for those? Otherwise, what happens > to the attributes once I have set them? Sorry, I responded too hastily and erroneously. Here's the proper, longer explanation. Assume the following models: class Referee(models.Model): name = models.CharField(max_length=255) class Referrer(models.Model): name = models.CharField(max_length=255) references = models.ManyToManyField(Referee) It is evident that a many-to-many relation (references) can be saved only after the object that has the relation (Referrer) is warranted to have a value for it's primary key field, as "behind the scenes, Django creates an intermediary join table to represent the many-to- many relationship" (quote from http://docs.djangoproject.com/en/dev/ref/models/fields/#manytomanyfield ) So, given a obj = Referrer("foo"), updating its many-to-many field can happen only after obj.save() returns (this is documented in http://www.djangoproject.com/documentation/models/many_to_many/ ). If you are not able to call obj.save() and then manually update the fields, the only chance is to use a signal. Unfortunately, Django core does not currently fire any signals on ManyToManyField changes. There's a ticket, http://code.djangoproject.com/ticket/5390 , that has a patch with a solid implementation of that particular signal. It also includes tests that demonstrate how to use the signal (see http://code.djangoproject.com/attachment/ticket/5390/complete-patch.diff ). So, unless the manual way works for you, I recommend you apply the patch. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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: overriding save() for ManyToManyFields
On Sep 23, 10:40 am, "Daniele Procida" wrote: > So, obviously I need another stage, to save the many-to-many relations > once theo bject is saved. Call super(Event, self).save(), then update the many-to-many relations. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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: QuerySet - filter through related objects
On Sep 17, 2:39 pm, filippo wrote: > Is it an official feature? I can't find it anywhere in > documentation... http://docs.djangoproject.com/en/dev/topics/db/queries/#chaining-filters --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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: need help for a realted queryset
On Sep 17, 1:04 pm, andreas schmid wrote: > hi, > > i have 3 models topic, project, technology. a topic contains projects > and projects have technolgies. > the 3 models are related to each other by foreignkeys in the project model. > > in the topic detail view i need to have the projects within the given > topic which works with a wrapper around the object_detail view (generic) > wich topic.projects_set.all() and a for loop in the template. > > i get the technologies for the projects too like this: > > {% for project in object_list %} > {% for technology in project.technologies.all %} > {{ > technology.title }} > {% if forloop.last %}{% else %} > {% ifequal forloop.revcounter0 1 %}and {% else %}, {% > endifequal %} > {% endif %} > {% endfor %} > {% endfor %} > > but of course the same technology shows up multiple times if more > projects use the same technology. > how can i get the technologies as a distinct list? > > do i have to get the queryset in the view? (if yes how?) o > r is there a way to get distinct results also in a template? Use distinct(), not all() (which evidently works in templates well for any queryset). --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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: return related objects from a queryset
On Sep 17, 8:21 am, Milan Andric wrote: > So is there a way I can query on Alumni.objects.filter(**query_params) > but then return the Profile objects related to the Alumni objects > rather than the Alumni objects? >>> query_params = {'grad_year': 2008, 'third_year': True} >>> profile_qp = dict(('alumni__' + key, val) for key, val in >>> query_params.items()) >>> profile_qp {'alumni__grad_year': 2008, 'alumni__third_year': True} >>> Profile.objects.filter(**profile_qp) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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: Check whether a model has a field with given name
On Sep 17, 9:55 am, Dmitry Risenberg wrote: > Is it by-design not to add fields to Model's __dict__? Yes. If you want to know the design better, you may find more insight in e.g. "Pro Django" book by Marty Alchin, Malcolm Tredinnick's "Inside the ORM" DjangoCon 2008 presentation and -- last not least -- in the source. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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: Aggregate using extra() fields
On Sep 16, 11:30 pm, Kathleen Benitez wrote: > I am attempting to evaluate some generalization schemes using Django. > In particular, I'd like to create a new field (generalized race), and > then find a total population using the values() function as follows: annotate() is somewhat confusing in your example, seeing the Record model and explicitly stating what you want to achieve with it would help us to help you better. > 1 r = Records.objects > 2 r = r.extra(select={'generalized_race: "CASE race WHEN 'Black' THEN > 'B' WHEN 'White' THEN 'W' ELSE 'O' END"}) This is indeed "registered with Django": >>> rs = Record.objects.extra(select={'generalized_race': ... "CASE race WHEN 'Black' THEN'B' WHEN 'White' THEN 'W' ELSE 'O' END"}) >>> rs[0].generalized_race u'B' However, it is correct that although extra(select={'foo': ...}) augments the results with the field foo, it is not a proper models.Field and can not be referenced in further QuerySet methods. > 3 fields = ['age', 'sex', 'generalized_race'] > 4 r = r.values(*fields).annotate(Pop = Sum('population')) Are you sure you need the values() call here? If you really don't want to select other fields, only() works equally well and retains the generalized_race pseudo-field: >>> fields = ['age', 'sex'] >>> rs.only(*fields)[0].generalized_race u'B' As for the annotate() clause, it is unclear where the population comes from and what's the intent. Please clarify. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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: Check whether a model has a field with given name
On Sep 16, 9:31 pm, Dmitry Risenberg wrote: > try: > field_attribute = getattr(self.model, self.field_name) > except AttributeError: try: self.model._meta.get_field_by_name(self.field_name) except models.FieldDoesNotExist: ... --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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: Django critter helps us code at the speed of light.
On Mar 16, 3:53 pm, Torsten Bronger wrote > No, I mean "contained in the original critter". As far as I can > see, you added the glory. I simply wonder why. Ah, sorry for the misunderstanding :). It looked nice, conveys the "helpful fellow" message and -- last not least -- helps me to add 3D- depth to the figure. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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: Django critter helps us code at the speed of light.
On Mar 16, 2:55 pm, Torsten Bronger wrote: > Hallöchen! > > mrts writes: > > On Mar 15, 5:56 pm, mrts wrote: > > >> I don't, therefore I'm all for the critter and my (styled) take > >> is here:http://mrts-foo.appspot.com/ > > > For anyone still interested in the critter, there are some banners > > now athttp://mrts-foo.appspot.com/. > > But the glory is not authentic, is it? If by "authentic" you mean "endorsed by core devs" -- no. That is explicitly stated at http://mrts-foo.appspot.com/ (and I made it even more explicit just now). But neither is the pony "authentic" in that sense, Django does not have an official mascot AFAICT. That's the reason why the name "Django" is not used in neither the critter nor the pony banners. If there are any issues with the current wording in that regard at http://mrts-foo.appspot.com/ , please let me know. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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: Django critter helps us code at the speed of light.
On Mar 15, 5:56 pm, mrts wrote: > I don't, therefore I'm all for the critter and my (styled) take is > here:http://mrts-foo.appspot.com/ For anyone still interested in the critter, there are some banners now at http://mrts-foo.appspot.com/ . --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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: Django critter helps us code at the speed of light.
On Mar 12, 1:24 pm, Nick wrote: > On Mar 11, 6:10 pm, Eric Walstad wrote: > > Now you can have your pony AND a Django > > Critter:http://starship.python.net/~ewalstad/django_critter.html > > Excellent! I think I prefer the Critter to the Pony... I takes a brave man to identify himself with the grrly Pony, so thumbs up to everybody who have that much bravado in themselves :) I don't, therefore I'm all for the critter and my (styled) take is here: http://mrts-foo.appspot.com/ (Created with Inkscape, but Illustrator is also able to open SVGs). --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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: Feel free to test queryset-refactor branch
Big cheers and big thanks! Has anyone tried merging qs-rf and nf-admin already? MS --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---