Hey Dylan, sorry again. My situation is a bit more complex. Let me try clarifying...
I have... obj = apps.get_model(app_label="myApp", model_name="myModel") But I cannot do... obj.setattr() I may be going about this wrong, but my script asks the user to type the name of the model they want to access into the terminal, hence the `obj` line. Then the script asks the user to type in the column name they want to access. So if the user inputs "myColumn", I'd like to be able to do something like `obj.myColumn = "Some value"`. Is this possible, or am I on the wrong track? On Monday, January 22, 2018 at 9:02:46 PM UTC-5, Dylan Reinhold wrote: > > Andrew, > You do not to get the field with _meta.get_field just use setattr which > takes a string as the field name. > Now if you are creating a new instance of your model and are just passing > a single field all other fields would need to have defaults or be null=True. > > In your do stuff area you can just run this to create a new object with > the string 'Test Text': > > model_instance = model() > setattr(model_instance, column_name, 'Test Text') > model_instance.save() > > > Dylan > > > On Mon, Jan 22, 2018 at 5:32 PM, Tom Tanner <dontsende...@gmail.com > <javascript:>> wrote: > >> Darn, is this possible with a new object of the model? My idea is to in >> the end let the user input information that will be made into a new record >> to add to the model. But I need the user to type in the model they want to >> use... >> >> >> On Monday, January 22, 2018 at 1:09:53 PM UTC-5, Andrew Standley wrote: >> >>> 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: >>> >>> 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. >>> >>> >>> 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...@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 >>> <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 >>> >>> <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:* *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 >>> *asta...@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...@googlegroups.com <javascript:>. >> To post to this group, send email to django...@googlegroups.com >> <javascript:>. >> 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 >> >> <https://groups.google.com/d/msgid/django-users/35c6c459-3f75-4274-b702-de0d7d5ac058%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> >> For more options, visit https://groups.google.com/d/optout. >> > > -- 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/7300e8ee-2cdb-4d24-9852-a4f8b1753c20%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.