Note that there are some explanatory texts on larger screens.

plurals
  1. POFlexible field list names in django models class
    text
    copied!<p>Instead of dynamically altering a models file by adding fields, very bad i've been told, i'm suppose to maintain a type of flexibility by having variable field list names(i think).</p> <p>Thus, when an attribute is added to the database, this attribute can be accessed without the models file being altered.</p> <p>I cant figure out how to create variable field list names in my models class though. I'm having trouble sifting through reading materials to find a solution to my problem, and trial and era is 15hrs and counting.</p> <p>Could some one point me in the right direction. </p> <p>New Edit</p> <p>Heres what im trying to achieve. </p> <p>When an attribute is added, i add it to the table like this.</p> <pre><code> c = 'newattributename' conn = mdb.connect('localhost', 'jamie', '########', 'website') cursor = conn.cursor() cursor.execute("alter table mysite_weightsprofile add column %s integer not null; SET @rank=0; UPDATE mysite_weightsprofile SET %s = @rank:=@rank+1 order by %s DESC;" % (c, c, a)) cursor.close() conn.close() </code></pre> <p>Now, in my models class i have</p> <pre><code>class WeightsProfile(models.Model): 1attributes = models.IntegerField() 2attributes = models.IntegerField() 3attributes = models.IntegerField() class UserProfile(WeightsProfile): user = models.ForeignKey(User, unique=True) aattributes = models.CharField() battributes = models.CharField() cattributes = models.CharField() </code></pre> <p>Now all i want to do is get access to the new attribute that was added in the table but not added to in the models file. </p> <p>Does sberry2A have the right answer. I hope it is, it seems the simplest.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload