Re: Overriding methods in models of other developers

2019-07-27 Thread Jani Tiainen
You  can do that with proxy models which allows you to override and add
methods.

Of course if there is a need to really modify original model it gets tricky.

la 27. heinäk. 2019 klo 10.49 אורי  kirjoitti:

> Hi,
>
> I had to change the following lines because tests failed:
>
> class Friend1(object):
> def __str__(self):
> return "User {} is friends with {}".format(self.to_user, 
> self.from_user)
>
>
> class FriendshipRequest1(object):
> def __str__(self):
> return "Friendship request from user {} to {}".format(self.from_user, 
> self.to_user)
>
>
> Friend.__str__ = Friend1.__str__
> FriendshipRequest.__str__ = FriendshipRequest1.__str__
>
>
> אורי
> u...@speedy.net
>
>
> ‪On Sat, Jul 27, 2019 at 9:26 AM ‫אורי‬‎  wrote:‬
>
>> Django users,
>>
>> I want to override def __str__ of models I'm using, but the models are
>> maintained by other developers. I think the __str__ is only used by the
>> admin interface. I found out that I can do something like this:
>>
>> from django.contrib import admin
>> from django.contrib.sites.models import Site
>> from django.contrib.auth.models import Group
>>
>> from friendship.models import Follow, Friend, FriendshipRequest, Block
>>
>>
>> class ReadOnlyModelAdmin(admin.ModelAdmin):
>> """
>> ModelAdmin class that prevents modifications through the admin.
>>
>> The changelist and the detail view work, but a 403 is returned
>> if one actually tries to edit an object.
>> """
>> actions = None
>>
>> # We cannot call super().get_fields(request, obj) because that method 
>> calls
>> # get_readonly_fields(request, obj), causing infinite recursion. Ditto 
>> for
>> # super().get_form(request, obj). So we assume the default ModelForm.
>> def get_readonly_fields(self, request, obj=None):
>> return self.fields or [f.name for f in self.model._meta.fields]
>>
>> def has_add_permission(self, request):
>> return False
>>
>> # Allow viewing objects but not actually changing them.
>> def has_change_permission(self, request, obj=None):
>> return (request.method in ['GET', 'HEAD'] and 
>> super().has_change_permission(request, obj))
>>
>> def has_delete_permission(self, request, obj=None):
>> return False
>>
>>
>> admin.site.unregister(Site)
>> admin.site.register(Site, ReadOnlyModelAdmin)
>>
>> admin.site.unregister(Group)
>> # admin.site.register(Group, ReadOnlyModelAdmin)
>>
>> admin.site.unregister(Block)
>> admin.site.unregister(Follow)
>> admin.site.unregister(Friend)
>> admin.site.unregister(FriendshipRequest)
>> # admin.site.register(Block, ReadOnlyModelAdmin)
>> # admin.site.register(Follow, ReadOnlyModelAdmin)
>> admin.site.register(Friend, ReadOnlyModelAdmin)
>> admin.site.register(FriendshipRequest, ReadOnlyModelAdmin)
>>
>>
>> class Friend1(Friend):
>> def __str__(self):
>> return "User {} is friends with {}".format(self.to_user, 
>> self.from_user)
>>
>>
>> class FriendshipRequest1(FriendshipRequest):
>> def __str__(self):
>> return "Friendship request from user {} to 
>> {}".format(self.from_user, self.to_user)
>>
>>
>> Friend.__str__ = Friend1.__str__
>> FriendshipRequest.__str__ = FriendshipRequest1.__str__
>>
>> But, is it possible override __str__ in a cleaner way? It seems to me not
>> such a clean way to override a method (but it works). But I have to use the
>> model itself, because there is a lot of code in the package I'm using that
>> uses the model itself.
>>
>> (We are using our own Block model which I think we developed before they
>> developed a similar model. We are not using their Follow model too).
>>
>> אורי
>> u...@speedy.net
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CABD5YeHNUEozN0vg_M5Hz9QrCTUAzH--u9SHtPVeDvJ4ZgWeOA%40mail.gmail.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHn91odhu6Dtaxq%3DtVA5vmWSPmJUyRL42o--h--awBm3BHVjpw%40mail.gmail.com.


Re: Overriding methods in models of other developers

2019-07-27 Thread אורי
Hi,

I had to change the following lines because tests failed:

class Friend1(object):
def __str__(self):
return "User {} is friends with {}".format(self.to_user, self.from_user)


class FriendshipRequest1(object):
def __str__(self):
return "Friendship request from user {} to
{}".format(self.from_user, self.to_user)


Friend.__str__ = Friend1.__str__
FriendshipRequest.__str__ = FriendshipRequest1.__str__


אורי
u...@speedy.net


‪On Sat, Jul 27, 2019 at 9:26 AM ‫אורי‬‎  wrote:‬

> Django users,
>
> I want to override def __str__ of models I'm using, but the models are
> maintained by other developers. I think the __str__ is only used by the
> admin interface. I found out that I can do something like this:
>
> from django.contrib import admin
> from django.contrib.sites.models import Site
> from django.contrib.auth.models import Group
>
> from friendship.models import Follow, Friend, FriendshipRequest, Block
>
>
> class ReadOnlyModelAdmin(admin.ModelAdmin):
> """
> ModelAdmin class that prevents modifications through the admin.
>
> The changelist and the detail view work, but a 403 is returned
> if one actually tries to edit an object.
> """
> actions = None
>
> # We cannot call super().get_fields(request, obj) because that method 
> calls
> # get_readonly_fields(request, obj), causing infinite recursion. Ditto for
> # super().get_form(request, obj). So we assume the default ModelForm.
> def get_readonly_fields(self, request, obj=None):
> return self.fields or [f.name for f in self.model._meta.fields]
>
> def has_add_permission(self, request):
> return False
>
> # Allow viewing objects but not actually changing them.
> def has_change_permission(self, request, obj=None):
> return (request.method in ['GET', 'HEAD'] and 
> super().has_change_permission(request, obj))
>
> def has_delete_permission(self, request, obj=None):
> return False
>
>
> admin.site.unregister(Site)
> admin.site.register(Site, ReadOnlyModelAdmin)
>
> admin.site.unregister(Group)
> # admin.site.register(Group, ReadOnlyModelAdmin)
>
> admin.site.unregister(Block)
> admin.site.unregister(Follow)
> admin.site.unregister(Friend)
> admin.site.unregister(FriendshipRequest)
> # admin.site.register(Block, ReadOnlyModelAdmin)
> # admin.site.register(Follow, ReadOnlyModelAdmin)
> admin.site.register(Friend, ReadOnlyModelAdmin)
> admin.site.register(FriendshipRequest, ReadOnlyModelAdmin)
>
>
> class Friend1(Friend):
> def __str__(self):
> return "User {} is friends with {}".format(self.to_user, 
> self.from_user)
>
>
> class FriendshipRequest1(FriendshipRequest):
> def __str__(self):
> return "Friendship request from user {} to {}".format(self.from_user, 
> self.to_user)
>
>
> Friend.__str__ = Friend1.__str__
> FriendshipRequest.__str__ = FriendshipRequest1.__str__
>
> But, is it possible override __str__ in a cleaner way? It seems to me not
> such a clean way to override a method (but it works). But I have to use the
> model itself, because there is a lot of code in the package I'm using that
> uses the model itself.
>
> (We are using our own Block model which I think we developed before they
> developed a similar model. We are not using their Follow model too).
>
> אורי
> u...@speedy.net
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CABD5YeHNUEozN0vg_M5Hz9QrCTUAzH--u9SHtPVeDvJ4ZgWeOA%40mail.gmail.com.


Overriding methods in models of other developers

2019-07-27 Thread אורי
Django users,

I want to override def __str__ of models I'm using, but the models are
maintained by other developers. I think the __str__ is only used by the
admin interface. I found out that I can do something like this:

from django.contrib import admin
from django.contrib.sites.models import Site
from django.contrib.auth.models import Group

from friendship.models import Follow, Friend, FriendshipRequest, Block


class ReadOnlyModelAdmin(admin.ModelAdmin):
"""
ModelAdmin class that prevents modifications through the admin.

The changelist and the detail view work, but a 403 is returned
if one actually tries to edit an object.
"""
actions = None

# We cannot call super().get_fields(request, obj) because that method calls
# get_readonly_fields(request, obj), causing infinite recursion. Ditto for
# super().get_form(request, obj). So we assume the default ModelForm.
def get_readonly_fields(self, request, obj=None):
return self.fields or [f.name for f in self.model._meta.fields]

def has_add_permission(self, request):
return False

# Allow viewing objects but not actually changing them.
def has_change_permission(self, request, obj=None):
return (request.method in ['GET', 'HEAD'] and
super().has_change_permission(request, obj))

def has_delete_permission(self, request, obj=None):
return False


admin.site.unregister(Site)
admin.site.register(Site, ReadOnlyModelAdmin)

admin.site.unregister(Group)
# admin.site.register(Group, ReadOnlyModelAdmin)

admin.site.unregister(Block)
admin.site.unregister(Follow)
admin.site.unregister(Friend)
admin.site.unregister(FriendshipRequest)
# admin.site.register(Block, ReadOnlyModelAdmin)
# admin.site.register(Follow, ReadOnlyModelAdmin)
admin.site.register(Friend, ReadOnlyModelAdmin)
admin.site.register(FriendshipRequest, ReadOnlyModelAdmin)


class Friend1(Friend):
def __str__(self):
return "User {} is friends with {}".format(self.to_user, self.from_user)


class FriendshipRequest1(FriendshipRequest):
def __str__(self):
return "Friendship request from user {} to
{}".format(self.from_user, self.to_user)


Friend.__str__ = Friend1.__str__
FriendshipRequest.__str__ = FriendshipRequest1.__str__

But, is it possible override __str__ in a cleaner way? It seems to me not
such a clean way to override a method (but it works). But I have to use the
model itself, because there is a lot of code in the package I'm using that
uses the model itself.

(We are using our own Block model which I think we developed before they
developed a similar model. We are not using their Follow model too).

אורי
u...@speedy.net

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CABD5YeFkjgbYD0vatfC3%3DAQ8zKCYYNkwdNgpCdd6_%3DiEkc6dgA%40mail.gmail.com.


Re: Methods in Models

2016-10-26 Thread Deep Shah
This makes sense! Thanks!

On Monday, October 24, 2016 at 1:17:26 PM UTC+5:30, Deep Shah wrote:
>
> What kind of methods should be part of the models and what should be in 
> the views? Can anyone give me an example of a method which should be in a 
> Model than the views file?
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c3bec412-57db-4732-91b6-b53cc39d2912%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Methods in Models

2016-10-24 Thread Asad Jibran Ahmed
Any method that works *only* with the model data and is used in multiple
places in the application should be a part of the model.

Next, if you have functions that operate on multiple pieces of data (or
more than 1 model instances) you should put them in the view where they are
used. But if these functions are needed from multiple views, then
personally I create a helper class to hold them, and the helper class is
outside the view and model files.

Of course in the end, none of these are hard and fast rules. Just go with
whatever you feel is more maintainable, and use these as general guidelines.

Asad Jibran Ahmed 
http://blog.asadjb.com

On Mon, Oct 24, 2016 at 11:47 AM, Deep Shah  wrote:

> What kind of methods should be part of the models and what should be in
> the views? Can anyone give me an example of a method which should be in a
> Model than the views file?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/3ccf6df1-aaa2-42a7-b5dc-ecb8f2390bf5%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2BYYaWekXL_Qveb1xUeM1fFdNdpmNu9fvoYO9_zJUBn4qOGk3Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Methods in Models

2016-10-24 Thread Deep Shah
What kind of methods should be part of the models and what should be in the 
views? Can anyone give me an example of a method which should be in a Model 
than the views file?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/3ccf6df1-aaa2-42a7-b5dc-ecb8f2390bf5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Returning values from calculations done by custom methods in models

2012-02-18 Thread Gchorn
Whoops, wow that really was basic Python.  Sorry for the spam; of
course I know there's a difference between calling a method and
calling an attribute...how embarassing. =)

Thanks for your help Anssi.  Also thanks for not adding, ", you
moron!" to the end of each sentence...

On Feb 19, 9:47 am, akaariai  wrote:
> On Feb 19, 3:38 am, Gchorn  wrote:
>
>
>
>
>
>
>
>
>
> > Hello All,
>
> > So in my models.py file, I have:
>
> > class Player(models.Model):
> >         team = models.ForeignKey(Team)
> >         first_name = models.CharField(max_length=100)
> >         last_name = models.CharField(max_length=100)
> >         gp = models.IntegerField(max_length=2) #games played
> >         mp = models.IntegerField(max_length=4) #minutes played
> >         def mpg(self): #minutes per game
> >                 return self.mp/self.gp
> >         def __unicode__(self):
> >                 return self.first_name+' '+self.last_name
>
> > When I run "python manage.py shell" and try to pull up a player's
> > "mpg", I get:
>
> > >>> p = Player.objects.get(last_name='Durant')
> > >>> p
>
> > >>> p.mp
> > 1027
> > >>> p.gp
> > 27
> > >>> p.mpg
>
> > >
>
> > How do I get the API to return '38' and not  > blah>?  I realize this probably has something to do with telling the
> > models.py file how to represent p.mpg with some kind of __unicode__()
> > method, but I don't know exactly what that code should be or where it
> > should go relative to the "def mpg(self)" method...
>
> mpg is a method, so call it:
> p.mpg()
>
> If you want to have p.mpg returning the value, you could use a
> property:
> def _get_mpg(self):
>     return self.mp/self.gp
> mpg = property(_get_mpg)
>
> There isn't anything special about models.py in this regard, this is
> just standard Python. So, I suggest learning a little more about
> Python. If you go through trial-error learning here you are in for a
> long ride.
>
>  - Anssi

-- 
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: Returning values from calculations done by custom methods in models

2012-02-18 Thread akaariai
On Feb 19, 3:38 am, Gchorn  wrote:
> Hello All,
>
> So in my models.py file, I have:
>
> class Player(models.Model):
>         team = models.ForeignKey(Team)
>         first_name = models.CharField(max_length=100)
>         last_name = models.CharField(max_length=100)
>         gp = models.IntegerField(max_length=2) #games played
>         mp = models.IntegerField(max_length=4) #minutes played
>         def mpg(self): #minutes per game
>                 return self.mp/self.gp
>         def __unicode__(self):
>                 return self.first_name+' '+self.last_name
>
> When I run "python manage.py shell" and try to pull up a player's
> "mpg", I get:
>
> >>> p = Player.objects.get(last_name='Durant')
> >>> p
>
> >>> p.mp
> 1027
> >>> p.gp
> 27
> >>> p.mpg
>
> >
>
> How do I get the API to return '38' and not  blah>?  I realize this probably has something to do with telling the
> models.py file how to represent p.mpg with some kind of __unicode__()
> method, but I don't know exactly what that code should be or where it
> should go relative to the "def mpg(self)" method...

mpg is a method, so call it:
p.mpg()

If you want to have p.mpg returning the value, you could use a
property:
def _get_mpg(self):
return self.mp/self.gp
mpg = property(_get_mpg)

There isn't anything special about models.py in this regard, this is
just standard Python. So, I suggest learning a little more about
Python. If you go through trial-error learning here you are in for a
long ride.

 - Anssi

-- 
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.



Returning values from calculations done by custom methods in models

2012-02-18 Thread Gchorn
Hello All,

So in my models.py file, I have:

class Player(models.Model):
team = models.ForeignKey(Team)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
gp = models.IntegerField(max_length=2) #games played
mp = models.IntegerField(max_length=4) #minutes played
def mpg(self): #minutes per game
return self.mp/self.gp
def __unicode__(self):
return self.first_name+' '+self.last_name

When I run "python manage.py shell" and try to pull up a player's
"mpg", I get:

>>> p = Player.objects.get(last_name='Durant')
>>> p

>>> p.mp
1027
>>> p.gp
27
>>> p.mpg
>

How do I get the API to return '38' and not ?  I realize this probably has something to do with telling the
models.py file how to represent p.mpg with some kind of __unicode__()
method, but I don't know exactly what that code should be or where it
should go relative to the "def mpg(self)" method...

-- 
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.