开发者

update foreignkey items from django shell

Following is the models.py:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    belongs_to_user_category = models.ForeignKey(UserCustomCategory, null=True, blank=True)

class UserHistory(models.Model):
    date_time = models.DateTimeField()
    user = models.ForeignKey(User开发者_如何学运维Profile, null=True, blank=True)
    points_earned = models.DecimalField(max_digits=5, decimal_places=3)

as is clear, userhistory is a foreign key to UserProfile. For the test purpose i wanted to update the points of the user whose name starts with a

I wrote the following code in the python shell:

from myapp.models import *
uobj = UserProfile.objects.all()
for i in uobj:
    if i.user.username[0] == 'a':
        b = UserHistory.objects.create(user=i)
        b.points_earned = random.random(10, 100)
        b.date_time = datetime.datetime.now()
        b.save()

i have also tried b = UserHistory.objects.get_or_create(user=i) with same error and i get the following error:

ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (160, 0))

ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (13, 0))

ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (63, 0))

ERROR: Internal Python error in the inspect module.
Below is the traceback from this internal error.

Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/IPython/ultraTB.py", line 667, in text
    locals,formatvalue=var_repr))
  File "/usr/lib/python2.6/inspect.py", line 875, in formatargvalues
    specs.append(strseq(args[i], convert, join))
  File "/usr/lib/python2.6/inspect.py", line 830, in strseq
    return convert(object)
  File "/usr/lib/python2.6/inspect.py", line 872, in convert
    return formatarg(name) + formatvalue(locals[name])
KeyError: 'connection'

IPython's exception reporting continues...

---------------------------------------------------------------------------
IntegrityError                            Traceback (most recent call last)


IntegrityError: (1048, "Column 'date_time' cannot be null")


When you use the create method of the default model manager in Django, it will also attempt to create that instance into the database and save it. You can do this in two ways, but I'll show you what you can do with your approach, which may help in some understanding.

First, you will want to create a UserHistory object, but don't actually save it. This is done by simply instantiating that model class with any default values you'd like:

b = UserHistory(user=i)

After that, you can set the other attributes.

b.points_earned = random.randint(10, 100)
b.date_time = datetime.datetime.now()
b.save()

And this will work. Because you're now saving b after you've set the date_time.

There are ways to improve this of course, you can simply create everything in one call since you're doing it in one logical step, like so:

b = UserHistory.objects.create(
  user=i,
  points_earned=random.randint(10, 100),
  date_time=datetime.datetime.now(),
)

You can further improve this by reading the DateField docs for Django, which will give you some tips on how to set the default date to the current time.

Hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜