0

What I have currently is a CharField for clients which of course might pose issues in the future when I'm not the only person entering job data. I don't want to misspell a client name and mess up my web app, additionally, if a client changes their name I'd want the app to automatically change the name of the client on previous jobs.

I created a new model

class Clients(models.Model):
    client = models.CharField(max_length=120, blank=False, null=True)

def __unicode__(self): #python 3.3. is __str__
    return self.client`

and added all of the clients to it. I want to be able to now have

from django.db import models

a = allclients #pseudocode

class Jobs(models.Model):
    client = models.CharField(max_length=120,choices=a,blank=True, null=True)#testing

How do I get 'a' to actually populate and how do I make sure that if a client name is changed in Clients(models.Model) that it updates everything I'm doing with my Jobs(models.Model)?

EDIT

I have attempted to add a foreign key. By doing

class Jobs(models.Model):
    # ... other stuff
    client = models.ForeignKey('Clients')

I flushed my database and started over because I was having issues migrating and I'm now getting an error that says:

Exception Value:    
no such column: SiteTwo_jobs.client_id
click here
  • 814
  • 2
  • 10
  • 24
  • Possible duplicate of [Creating a dynamic choice field](http://stackoverflow.com/questions/3419997/creating-a-dynamic-choice-field) – RickyA Nov 27 '15 at 22:27
  • Why don't you use a foreign key? – Daniel Roseman Nov 27 '15 at 22:30
  • I don't really know what a foreign key is, having just now googled it, I have a vague understanding. I'm not sure how it would be used in my case though as I'm a bit new to this. – click here Nov 27 '15 at 22:32
  • @DanielRoseman, I think i'm close here but I'm getting this instead of the actual client names in my dropdown. http://i.imgur.com/Qg96GL4.png – click here Nov 27 '15 at 23:55
  • Define a `__unicode__` method on Client (or `__str__` if you're using Python 3) to return the name field. – Daniel Roseman Nov 28 '15 at 09:15
  • Thank you, I clearly don't understand this portion of models as I've been putting 'whatever' there. – click here Nov 30 '15 at 13:37

0 Answers0