1

New to Django coming from .NET with an architectural question.

Inside my models.py, I have a concept called city. These cities can be enabled/disabled.

Inside my views, I want to retrieve all active cities under my view called Cities. I need to retrieve all active cities in many places, so I thought I'd make a method inside my models.py city class called get_in_country, so it looks like this:

class City(models.Model):
    title = models.CharField(max_length=200)
    alias = models.CharField(max_length=200)
    country = models.ForeignKey(Country, null=True)
    is_visible = models.BooleanField(default=False)

    def __str__(self):
        return self.title

    def get_in_country(self, country_id):
        #return best code ever seen

Anyway, my question now is: how do I use this inside views.py?

Being an awesome noob, I of course tried this:

def country(request, alias):
    cities_in_country = City.get_in_country(1) #whatever id

    data = {
            'cities_in_country': cities_in_country, 
        }

    return render(request, 'country.html', data)

Now, you don't have to be Einstein (ahem, Jon Skeet?) to realize this will go wrong, as I haven't made an instance of City and will cause an exception:

unbound method get_in_country() must be called with City instance as first argument (got int instance instead)

So: how would you modify my code to use my new awesome submethod?

1
  • Since you want to filter all rows in the City table by Country, a custom Manager method like City.objects.filter_by_country(country) would be best pratice. Commented May 15, 2015 at 6:38

1 Answer 1

1

You need to define get_in_country as a static function

By adding a decorator

@staticmethod

just before the class defenition as

@staticmethod 
    def get_in_country(self, country_id):

class City(models.Model):
    title = models.CharField(max_length=200)
    alias = models.CharField(max_length=200)
    country = models.ForeignKey(Country, null=True)
    is_visible = models.BooleanField(default=False)

    def __str__(self):
        return self.title

    @staticmethod # Changed here
    def get_in_country(self, country_id):
Sign up to request clarification or add additional context in comments.

5 Comments

Ah, static methods. The methods I always avoid in .NET ;-) When doing a static method, I assume you remove "self" ? If not, what do you provide as self?
You need to provide any self as argument. If you need to acess some class variables, pass the instance instead
But lets say my new static method is 'def get_in_country(self, country_id):', and I call it by saying City.get_in_country(current_country.id) - I will get an "get_in_country() takes exactly 2 arguments (1 given)" error. How to fix that?
@LarsHoldgaard No, you should be loosing self altogether. That is the def must look like def get_in_country(country_id):. For example ideone.com/wLA3g8
Great! Thanks a lot - it makes sense! :) Really a big help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.