Opened 6 years ago

Closed 6 years ago

#9861 closed (wontfix)

Requests make feature: Using a subset of fields on the Model

Reported by: freeren Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords: Using a subset of fields on the Model
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

The classes inheritance are useful when you want to put some common information into a number of other models.

class FlatPage(models.Model):
    url = models.CharField(_('URL'), max_length=100, db_index=True)
    title = models.CharField(_('title'), max_length=200)
    content = models.TextField(_('content'), blank=True)
    enable_comments = models.BooleanField(_('enable comments'))
    template_name = models.CharField(_('template name'), max_length=70, blank=True,
        help_text=_("Example: 'flatpages/contact_page.html'. 
If this isn't provided, the system will use 'flatpages/default.html'."))
    registration_required = models.BooleanField(_('registration required'), help_text=_("If this is checked, only logged-in users will be able to view the page."))
    sites = models.ManyToManyField(Site)


class SubFlatPage(models.Model):
    url = models.CharField(_('URL'), max_length=100, db_index=True)
    title = models.CharField(_('title'), max_length=200)
    content = models.TextField(_('content'), blank=True)
    sites = models.ManyToManyField(Site)
    other = models.CharField(_('other'), max_length=200)

Using a subset of fields on the model (abstract=True OR abstract=False )
like :
http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#using-a-subset-of-fields-on-the-form

# Use the fields attribute of the Model's inner Meta class. This attribute, if given, should be a list of field names to include in the SubModel.

# Use the exclude attribute of the Model's inner Meta class. This attribute, if given, should be a list of field names to exclude from the SubModel.

# Can append a little other fields.

Change History (1)

comment:1 Changed 6 years ago by mtredinnick

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to wontfix
  • Status changed from new to closed

This isn't a common enough requirement to justify all the extra code it would require. The solution is to just create a separate model that contains the fields you need and set the Meta.db_table attribute to point to the other model's database table. You'll also need the support from #3163 (unless you avoid running syncdb for that app), but that should be in before Django 1.1 is released.

Note: See TracTickets for help on using tickets.
Back to Top