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/#
-Andrew
On 1/21/2018 9:44 PM, Tom Tanner wrote:
I'm making a terminal command for my Django app:
|
fromdjango.core.management.baseimportBaseCommand,CommandError
fromdjango.core.exceptions importFieldDoesNotExist
fromdjango.apps importapps
classCommand(BaseCommand):
defadd_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,
)
defhandle(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)
exceptLookupErrorase:
msg ='The model "%s" under the app "%s" does not exist!'\
%(model_name,app_label)
raiseCommandError(msg)
try:
column =model._meta.get_field(column_name)
exceptFieldDoesNotExistase:
msg ='The column "%s" does not match!'%column_name
raiseCommandError(msg)
else:
print(column,type(column))
# Do stuff here with the column, model.
|
Right now, `column` is `<django.db.models.fields.IntegerField:
column_name>`. I want this instance of `model` to have `column_name`
set to `100`. How can I set and save this instance in this manner?
--
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+unsubscr...@googlegroups.com
<mailto:django-users+unsubscr...@googlegroups.com>.
To post to this group, send email to django-users@googlegroups.com
<mailto:django-users@googlegroups.com>.
Visit this group at *MailScanner has detected definite fraud in the
website at "groups.google.com". Do /not/ trust this website:*
https://groups.google.com/group/django-users
<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:*
https://groups.google.com/d/msgid/django-users/2caa4cd5-cb62-4bee-8e41-6182bfba792a%40googlegroups.com
<https://groups.google.com/d/msgid/django-users/2caa4cd5-cb62-4bee-8e41-6182bfba792a%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit *MailScanner has detected definite fraud in
the website at "groups.google.com". Do /not/ trust this website:*
https://groups.google.com/d/optout <https://groups.google.com/d/optout>.
--
This message has been scanned for viruses and dangerous content by
*E.F.A. Project* <http://www.efa-project.org>, and is believed to be
clean.
Click here to report this message as spam.
<http://lsefa1.linear-systems.com/cgi-bin/learn-msg.cgi?id=D18A7100A34.A4D02&token=c1815d1b106eaf419f281d66c338e38a>
--
*Andrew Standley*
/Senior Software Engineer/
Linear Systems
(909) 899-4345 *225
/astand...@linear-systems.com <mailto:astand...@linear-systems.com> /
--
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/8e6ba53d-6a8c-a02b-754f-b8e404ff977d%40linear-systems.com.
For more options, visit https://groups.google.com/d/optout.