Joff
Joff

Reputation: 12187

Should I use class based or function based views for this?

This will be a long post so bear with me. I think I came to the realization that I have to re-write a lot of code that I previously wrote in my models.

my models look like this:

class Word(models.Model):
    '''Word table for word, and frequency data'''

    language = models.ForeignKey(Language)
    name = models.CharField(max_length=50)
    frequency = models.IntegerField(default=0)
    rank = models.IntegerField(default=0)


    def __unicode__(self):
        value = u'%s' % (self.name)
        return value


    def wn_lookup(self):
        '''Calls the wordnet CLI for self'''    
        ...breaks up the output and returns the definitions as a list of tuples

    def wiktionary_lookup(self, wiktionary_prefix, driver):
        ...looks up the definition of the word on wiktionary
           returns data as a list

    def define(self, driver, choice, ask_input):
        ...Prints out a lot of options and asks the user to choose 
           what they want as a definition

           It then creates objects and calls other methods on a 
           subsequent definitions model

Note that the last method creates and calls methods of a subsequent definitions model.

I have a few models, all with methods that require a lot of user input to semi-automate the data entry. I believe I have done this stupidly and I should actually add them in the views, but I am a bit confused about how to do that.

  1. Should I use a class based or function based view?
  2. I guess I have to actually write forms and templates for all of these views?
  3. Is it possible that I should leave everything where it is (in models) and not put it in views?

Upvotes: 1

Views: 68

Answers (1)

Ivan Semochkin
Ivan Semochkin

Reputation: 8897

  1. You can use what you want, the benefit of Class Based Views is inheritance. You can extend your base View class and use it throughout your project. Also, it's quite fast to get operations working with Generic Class Based Views.

  2. Yes you should write a form for this model, to provide user input. Django provides shortcuts for forms, which save data based on a model. Model Forms automatically map all fields in your model with equivelent form fields.

  3. Django follows a pattern called Fat Models so it is completely fine to store operations for a model within its model definition.

Upvotes: 2

Related Questions