Re: Modify the value in a row before returning through a query
Another thread today just clued me in. Adding @property to the model method made it work. Thank you for helping out Javier, I provided the full example below in case someone else looks for this: import urlparse def stripdomain(url): bits = urlparse.urlparse(url) return bits[1] class yourmodel (models.Model): domain = models.URLField(...) @property def strippeddomain (self): return stripdomain(self.domain) Then from the shell this works: >>> url = yourmodel.objects.get(id=1) >>> url.strippeddomain u'yourdomain.com' On Aug 24, 3:37 pm, Merrickwrote: > url.stripdomain() returns: > > yourdomain.com > > Do you know if there is a way to have url.domain return what > stripdomain() did above? Otherwise I'll use a template filter and move > on. > > Merrick > > On Aug 24, 12:20 pm, Javier Guerra wrote: > > > On Mon, Aug 24, 2009 at 1:42 PM, Merrick wrote: > > url = yourmodel.objects.get(id=1) > > url.domain > > > u'http://yourdomain.com/' > > > what does url.stripdomain() return? > > > -- > > Javier > > --~--~-~--~~~---~--~~ 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: Modify the value in a row before returning through a query
On Mon, Aug 24, 2009 at 5:37 PM, Merrickwrote: > Do you know if there is a way to have url.domain return what > stripdomain() did above? Otherwise I'll use a template filter and move > on. no need for a filter, {{url.stripdomain}} will call url.stripdomain() or, you could set url.stripdomain() as a property: class ..: ... @property def stripdomain(self): . -- Javier --~--~-~--~~~---~--~~ 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: Modify the value in a row before returning through a query
Looks like we were writing at the same time. I also was not aware you could call a model method in a template without the parenthesis, that's where I was getting hung up. Again thank you. On Aug 24, 3:54 pm, Javier Guerrawrote: > On Mon, Aug 24, 2009 at 5:37 PM, Merrick wrote: > > Do you know if there is a way to have url.domain return what > > stripdomain() did above? Otherwise I'll use a template filter and move > > on. > > no need for a filter, {{url.stripdomain}} will call url.stripdomain() > > or, you could set url.stripdomain() as a property: > > class ..: > ... > @property > def stripdomain(self): > . > > -- > Javier --~--~-~--~~~---~--~~ 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: Modify the value in a row before returning through a query
url.stripdomain() returns: yourdomain.com Do you know if there is a way to have url.domain return what stripdomain() did above? Otherwise I'll use a template filter and move on. Merrick On Aug 24, 12:20 pm, Javier Guerrawrote: > On Mon, Aug 24, 2009 at 1:42 PM, Merrick wrote: > url = yourmodel.objects.get(id=1) > url.domain > > u'http://yourdomain.com/' > > what does url.stripdomain() return? > > -- > Javier --~--~-~--~~~---~--~~ 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: Modify the value in a row before returning through a query
On Mon, Aug 24, 2009 at 1:42 PM, Merrickwrote: url = yourmodel.objects.get(id=1) url.domain > u'http://yourdomain.com/' what does url.stripdomain() return? -- Javier --~--~-~--~~~---~--~~ 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: Modify the value in a row before returning through a query
Thank you Javier, for some reason I cannot get it working. Using your example, I noticed the return value is stripdomain, I am assuming that function would be outside of the model? Assuming the code you provided, I would expect that adding the code below, directly above the class yourmodel definition, would work: import urlparse def stripdomain(url): bits = urlparse.urlparse(url) return bits[1] However the domain is not stripped when querying as follows: >>> url = yourmodel.objects.get(id=1) >>> url.domain u'http://yourdomain.com/' Can you tell if I am missing something? On Aug 24, 11:05 am, Javier Guerrawrote: > On Mon, Aug 24, 2009 at 1:01 PM, Merrick wrote: > > I know how to parse the URL but am drawing a blank on how to > > accomplish this at the model level. > > add a function to the model, that returns the parsed field. > > class yourmodel (models.Model): > > domain = models.URLField(...) > > def strippeddoamin (self): > return stripdomain(self.domain) > > -- > Javier --~--~-~--~~~---~--~~ 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: Modify the value in a row before returning through a query
On Mon, Aug 24, 2009 at 1:01 PM, Merrickwrote: > I know how to parse the URL but am drawing a blank on how to > accomplish this at the model level. add a function to the model, that returns the parsed field. class yourmodel (models.Model): domain = models.URLField(...) def strippeddoamin (self): return stripdomain(self.domain) -- Javier --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Modify the value in a row before returning through a query
I have this in models.py: domain = models.URLField(...) Let's say one row in the table has this for domain: 'http://www.google.com' When the domain field is looked up, I would like to return: www.google.com I know how to parse the URL but am drawing a blank on how to accomplish this at the model level. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---