Ticket #128 (closed defect: fixed)

Opened 6 years ago

Last modified 6 years ago

Support class-level constants

Reported by: carl@… Owned by: andrew
Priority: major Milestone: 0.6
Component: migrations Version: 0.5
Keywords: Cc:

Description

I often define class-level constants for things like choices:

class MyModel(models.Model):
    (STATUS_DRAFT, STATUS_PUBLISHED) = range(2)
    ...
    status = models.IntegerField(default=STATUS_DRAFT, choices=...)

This causes breakage every time I freeze models or auto-create (via --model or --auto) any migration involving that field, because STATUS_DRAFT gets thrown blindly into the migration / frozen model as a default value, in a context where it has no definition.

Is there any reasonable solution to this, to prevent having to make irritating manual edits to every single migration? I haven't looked at it in depth; perhaps it's an unavoidable symptom of South's design choice to do things by parsing models.py rather than introspecting model objects.

Change History

comment:1 Changed 6 years ago by andrew

  • Status changed from new to assigned
  • Version set to 0.5
  • Milestone set to 0.6

My approach to solve this beforehand has to been to eliminate the choices-like keywords, but that's not going to work here, since default is really a necessary field (and I've done stuff like this before). Although it's certainly feasible to freeze class-level constants, it might well be slightly verbose. I'll have to give this one a play around and see how it feels.

Perhaps one day I'll try and get a patch into core that makes it possible to intropect fields properly, but that's a long way off, and still doesn't work with custom fields...

comment:2 Changed 6 years ago by flosch

Just for the record: my last approach and (maybe dirty) patch for the problem: http://github.com/flosch/django-south/commit/04ad23fb899b25805e8ea1da36d48333eb9eba46

This works for me and resolves all unhandled class-level constants into their values. Callables are not supported by my patch currently.

Hope this helps temporarily.

comment:3 Changed 6 years ago by andrew

  • Status changed from assigned to closed
  • Resolution set to fixed

r231 should fix this; constants will get resolved and frozen as their actual values at the time, and everything should be fine. You'll obviously need to recreate any affected migrations if you haven't solved it manually.

Note: See TracTickets for help on using tickets.