开发者

Django. user_id null & duplicate error

  • Django 1.3
  • Python 2.7

In fixing one error, I've developed another. I'm not even sure where to begin, I'm so frustrated.

I got my app to show up in the Django admin section, and when I click on "add", it brings up the form. When I fill it out & submit it, it results in this error:

Exception Value: (1048, "Column 'user_id' cannot be null")

Seems simple & clear, but if I add user_id to my admin.py, then it results in the error I just fixed. If I leave it out and use the code that fixes the old error, then I get this error. Here's the relevant code:

admin.py (I commented out the code I was using, as that caused the earlier error)

from timeslip.models import Timeslip
from django.contrib import admin

class TimeslipAdmin(admin.ModelAdmin):
    pass
    #fields = ['user_id','day','hours_as_sec','part_of_day','drove','gas_money','notes']

admin.site.register(Timeslip, TimeslipAdmin)

models.py

from django.db import models
from django.contrib.auth.models import User

# Create your models here.

class Timeslip(models.Model):
   user=models.ManyToManyField(User)
   day = models.DateField()
   hours_as_sec = models.PositiveIntegerField()
   part_of_day = models.CharField(max_length=16,choices=PART_O_DAY)
   drove = models.BooleanField(default=False)
   gas_money = models.DecimalField(max_digits=5,decimal_places=2)
   notes = models.TextField()

class UserProfile(models.Model):
   user = models.ForeignKey(User)
   url = models.URLField("Website", blank=True)
   position = models.CharField(max_length=50, blank=True)

User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])

If I make Timeslip's user=, into a ForeignKey(User) instead of a ManyToManyField(User), the error becomes:

Exception Value: (1062, "Duplicate entry '2' for key 'user_id'")

...which I understand, but that's exactly what I'm trying to do here now -- make multiple timeslips for each user.

How do I do that? I keep getting ridiculous errors no matter what I try, and it's hard finding documentation on this even though it seems like it woul开发者_Go百科d be a common & basic thing.

Update:

New question. Is Django caching stuff? I removed something from the model and reset everything but it still shows the old form. If it's showing me something it cached - How do I flush that cache?

Anyway, here's the output when I run manage.py sqlall timeslip

BEGIN;
CREATE TABLE `timeslip_timeslip` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `user_id` integer NOT NULL,
    `day` date NOT NULL,
    `hours_as_sec` integer UNSIGNED NOT NULL,
    `part_of_day` varchar(16) NOT NULL,
    `drove` bool NOT NULL,
    `gas_money` numeric(5, 2) NOT NULL,
    `notes` longtext NOT NULL
)
;
ALTER TABLE `timeslip_timeslip` ADD CONSTRAINT `user_id_refs_id_bb29929c` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`);
CREATE TABLE `timeslip_userprofile_user` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `userprofile_id` integer NOT NULL,
    `user_id` integer NOT NULL,
    UNIQUE (`userprofile_id`, `user_id`)
)
;
ALTER TABLE `timeslip_userprofile_user` ADD CONSTRAINT `user_id_refs_id_c73880a3` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`);
CREATE TABLE `timeslip_userprofile` (
     `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
     `url` varchar(200) NOT NULL,
     `company` varchar(50) NOT NULL
)
;
ALTER TABLE `timeslip_userprofile_user` ADD CONSTRAINT `userprofile_id_refs_id_d05b8c80` FOREIGN KEY (`userprofile_id`) REFERENCES `timeslip_userprofile` (`id`);
CREATE INDEX `timeslip_timeslip_fbfc09f1` ON `timeslip_timeslip` (`user_id`);
COMMIT;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜