Note that there are some explanatory texts on larger screens.

plurals
  1. POautopopulating child table related by OneToOne in Django
    primarykey
    data
    text
    <p>I'm pretty new to django, and I'm having trouble with a child table that is an extension of Django's built User model at django.contrib.auth.model. The child table is UserProfile, and inherits from User. (see below for my models)</p> <p>I can't really figure out how to put data in these tables except by the admin site, which clearly isn't good enough. </p> <p>I'll get my user object and assign it some variable say, <code>u = User.objects.get(username = 'jane_smith'</code> then try and reach the data in the child table with the command <code>up = u.userprofile</code>. I'm rewarded with the following error: <code>DoesNotExist: UserProfile matching query does not exist.</code></p> <p>I experimented getting around the problem by manually inputting data through the terminal, with <code>up = UserProfile(user_ptr = User.objects.get(username = 'jane_smith'), country = 'USA', num_meals_hosted = 0, cook_rating = 5, image = None, zipcode =91011)</code> followed by <code>up.save()</code></p> <p>However, I got the following stack trace</p> <pre><code>&gt;&gt;&gt; up.save() Traceback (most recent call last): File "&lt;console&gt;", line 1, in &lt;module&gt; File "/Library/Python/2.6/site-packages/django/db/models/base.py", line 460, in save self.save_base(using=using, force_insert=force_insert, force_update=force_update) File "/Library/Python/2.6/site-packages/django/db/models/base.py", line 504, in save_base self.save_base(cls=parent, origin=org, using=using) File "/Library/Python/2.6/site-packages/django/db/models/base.py", line 526, in save_base rows = manager.using(using).filter(pk=pk_val)._update(values) File "/Library/Python/2.6/site-packages/django/db/models/query.py", line 491, in _update return query.get_compiler(self.db).execute_sql(None) File "/Library/Python/2.6/site-packages/django/db/models/sql/compiler.py", line 869, in execute_sql cursor = super(SQLUpdateCompiler, self).execute_sql(result_type) File "/Library/Python/2.6/site-packages/django/db/models/sql/compiler.py", line 735, in execute_sql cursor.execute(sql, params) File "/Library/Python/2.6/site-packages/django/db/backends/util.py", line 34, in execute return self.cursor.execute(sql, params) File "/Library/Python/2.6/site-packages/django/db/backends/mysql/base.py", line 86, in execute return self.cursor.execute(query, args) File "build/bdist.macosx-10.6-universal/egg/MySQLdb/cursors.py", line 174, in execute self.errorhandler(self, exc, value) File "build/bdist.macosx-10.6-universal/egg/MySQLdb/connections.py", line 36, in defaulterrorhandler raise errorclass, errorvalue IntegrityError: (1062, "Duplicate entry '' for key 'username'") </code></pre> <p>Anyone help? Just want to find a way to put data in these tables through the API. Below are my models. Happy to post more info in comments if needed.</p> <pre><code>from django.db import models from django.contrib.auth.models import User class UserProfile(User): address = models.CharField(max_length=100, blank=True) city = models.CharField(max_length = 50, blank=True) state = models.CharField(max_length = 15, blank=True) zipcode = models.IntegerField(blank=True) country = models.CharField(max_length = 50, blank=True) num_meals_hosted = models.IntegerField() cook_rating = models.IntegerField() diet_restrict = models.CharField(max_length = 600, blank=True) blurb = models.CharField(max_length = 10000, blank=True) website = models.URLField(blank=True) image_height = models.IntegerField(blank = True) image_width = models.IntegerField(blank = True) def __unicode__(self): return self.id </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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