from django.core.management.base import BaseCommand, CommandError
from django.core.exceptions import FieldDoesNotExist
from django.apps import apps
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument(
"--app",
dest="app",
required=True,
)
parser.add_argument(
"--model",
dest="model",
required=True,
)
parser.add_argument(
"--col",
dest="col",
required=True,
)
def handle(self, *args, **options):
app_label = options.get('app')
model_name = options.get('model')
column_name = options.get('col')
try:
model = apps.get_model(app_label=app_label, model_name=model_name)
except LookupError as e:
msg = 'The model "%s" under the app "%s" does not exist!' \
% (model_name, app_label)
raise CommandError(msg)
try:
column = model._meta.get_field(column_name)
except FieldDoesNotExist as e:
msg = 'The column "%s" does not match!' % column_name
raise CommandError(msg)
else:
print(column, type(column))
# Do stuff here with the column, model.
Hey Tom,
First you'll need to create or get a particular instance of
your model using one of it's managers `model.objects` and a
query. Ex for a model with a unique 'name' charfield: `model_obj =
model.objects.get(name='MyObject')`
You can then use the column_name to set that attribute on the
instance `setattr(model_obj, column_name) = 100` and finally save
those changes `model_obj.save()`
See https://docs.djangoproject.com/en/1.11/topics/db/queries/#
--
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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at MailScanner has detected definite fraud in the website at "groups.google.com". Do not trust this website: MailScanner has detected definite fraud in the website at "groups.google.com". Do not trust this website: https://groups.google.com/group/django-users.
To view this discussion on the web visit MailScanner has detected definite fraud in the website at "groups.google.com". Do not trust this website: MailScanner has detected definite fraud in the website at "groups.google.com". Do not trust this website: https://groups.google.com/d/msgid/django-users/2caa4cd5-cb62-4bee-8e41-6182bfba792a%40googlegroups.com.
For more options, visit MailScanner has detected definite fraud in the website at "groups.google.com". Do not trust this website: MailScanner has detected definite fraud in the website at "groups.google.com". Do not trust this website: https://groups.google.com/d/optout.
--
This message has been scanned for viruses and dangerous content by
E.F.A. Project, and is believed to be clean.
Click here to report this message as spam.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@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/35c6c459-3f75-4274-b702-de0d7d5ac058%40googlegroups.com.