Monday, January 22, 2018

Re: How do I set the value of a model object's property after using `get_field()`?

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 <dontsendemailhereok@gmail.com> 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.
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.

--
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+unsubscribe@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/35c6c459-3f75-4274-b702-de0d7d5ac058%40googlegroups.com.

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+unsubscribe@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/CAHtg44D0wfooSBKwSpcDe_jYBeJSTjHzUZ83UiAa53W2ObLKuQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment