I have the following form:
class ModuleItemForm2(forms.ModelForm):
class Meta:
model = Module_item
fields = ('title', 'media', 'thumb', 'desc', 'default', 'player_option')
The model is:
class Module_item(models.Model):
title = models.CharField(max_length=100)
layout = models.CharField(max_length=5, choices=LAYOUTS_CHOICE)
media = models.CharField(help_text='Media url', max_length=500, blank=True, null=True)
conserv = models.ForeignKey(Conserv, help_text= 'Redirect to Conserv', blank=True, null=True)
conserve_section = models.CharField(max_length=100, help_text= 'Section within the redirected Conserv', blank=True, null=True)
parent = models.ForeignKey('self', help_text='Upper menu.', blank=True, null=True)
module = models.ForeignKey(Module, blank=True, null=True)
thumb = models.FileField(upload_to='sms/module_items/thumbs', blank=True, null=True)
desc = models.CharField(max_length=500, blank=True, null=True)
auto_play = models.IntegerField(help_text='Auto start play (miliseconds)', blank=True, null=True)
order = models.IntegerField(help_text='Display order', blank=True, null=True)
depth = models.IntegerField(help_text='The layout depth', blank=True, null=True)
flow_replace = models.IntegerField(blank=True, null=True)
default = models.IntegerField(help_text='The selected sub item (Note: Starting from 0)', blank=True, null=True)
player_options = models.CharField(max_length=1000, null=True, blank=True)
In my view I build form:
module_item_form2 = ModuleItemForm2()
print module_item_form2
And I get the following error on the print line:
'NoneType' object has no attribute 'label'
It works fine with django 1.0.2. I see the error only in django 1.1.
Do you have an idea what am I doing wrong?
Regards, Arshavski Alexander.
You have player_options in the model, but player_option in the list of form fields. Does it work if you add the s in the form fields tuple?
Related
When I run my Django project on production server, I have this error:
ProgrammingError at /admin/core/event/
column core_event.hometask does not exist
LINE 1: ..._event"."is_approved", "core_event"."event_type", "core_even...
What I should do to fix it? Now I haven't "hometask" field in my model:
class Event(models.Model):
name = models.CharField(max_length=100, null=True)
author = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
description = models.TextField(max_length=1000, blank=True, null=True)
date_start = models.DateTimeField(null=True, blank=True)
date_finish = models.DateTimeField(null=True, blank=True)
image = models.ImageField(
upload_to="event_images/", default='event_images/default.png', blank=True, null=True)
is_approved = models.BooleanField(null=True)
TYPE_CHOICES = (
('Event', "Мероприятие"),
('Lesson', "Урок"),
)
event_type = models.CharField(choices=TYPE_CHOICES, max_length=200, default="Мероприятие")
topics = ArrayField(models.CharField(max_length=200), blank=True, null=True)
materials = ArrayField(models.URLField(), blank=True, null=True)
possible_users = models.ManyToManyField("core.User", blank=True, related_name='possible_users')
actual_users = models.ManyToManyField("core.User", blank=True, related_name='actual_users')
classes = models.ManyToManyField("core.Class", blank=True, related_name='classes')
From what you posted, looks like the field existed in the model before but not anymore. However, your admin.py still has reference to the old field of hometask.
So, go to admin.py, search for hometask, and remove it.
I have a two models, Person and Video.
Below viewset allows me all methods like POST, PATCH, DELETE, GET.
class PersonViewSet(viewsets.ModelViewSet):
queryset = apis_models.Person.objects.all()
serializer_class = apis_serializers.PersonSerializer
permission_classes = [HasPermPage]
Video model is associated with person model through Target model which is my main problem. Each person has few videos.
Now what I need is when I do "/person/{id}/" I should get person details along with all videos details it associated with.
Please let me know what change needs to be done to above ViewSet.
Models and Serializer:
class Video(DFModel):
source = models.ForeignKey(Source, models.DO_NOTHING)
creator = models.ForeignKey(
Creator, models.DO_NOTHING, blank=True, null=True)
title = models.CharField(max_length=500)
views = models.IntegerField(blank=True, null=True)
likes = models.IntegerField(blank=True, null=True)
dislikes = models.IntegerField(blank=True, null=True)
tags = models.TextField(blank=True, null=True)
upload_date = models.DateTimeField(blank=True, null=True)
page_url = models.TextField(blank=True, null=True)
video_url = models.TextField(blank=True, null=True)
thumb_url = models.TextField(blank=True, null=True)
sfw = models.BooleanField(blank=True, null=True)
category = models.CharField(max_length=20, blank=True, null=True)
reviewed = models.TextField(blank=True, null=True)
severity = models.CharField(max_length=10, blank=True, null=True)
origin = models.CharField(max_length=20, blank=True, null=True)
organization_id = models.IntegerField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
class Person(models.Model):
name = models.CharField(unique=True, max_length=45)
nationality = models.CharField(max_length=50, blank=True, null=True)
profession = models.CharField(max_length=50, blank=True, null=True)
avatar = models.CharField(max_length=100, blank=True, null=True)
active_scraping = models.BooleanField(blank=True, null=True)
vuln_score = models.FloatField(blank=True, null=True, default=None)
reviewed = models.TextField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
class Target(DFModel):
person = models.ForeignKey(
Person, models.DO_NOTHING, blank=True, null=True)
video = models.ForeignKey(
'Video', models.DO_NOTHING, blank=True, null=True)
reviewed = models.TextField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
class Meta:
db_table = 'target'
class PersonSerializer(CustomSerializer):
class Meta:
model = apis_models.Person
fields = '__all__'
class TargetSerializer(CustomSerializer):
class Meta:
model = apis_models.Target
fields = '__all__'
extra_fields = ['monthly_growth', 'vulnerablility_score']
class VideoSerializer(CustomSerializer):
source = SourceSerializer(read_only=True)
creator = CreatorSerializer(read_only=True)
class Meta:
model = apis_models.Video
fields = '__all__'
It looks like you have a ManyToMany relation between Person and Video, so, I suggest you to change your models to be like:
class Person(models.Model):
name = models.CharField(unique=True, max_length=45)
nationality = models.CharField(max_length=50, blank=True, null=True)
profession = models.CharField(max_length=50, blank=True, null=True)
avatar = models.CharField(max_length=100, blank=True, null=True)
active_scraping = models.BooleanField(blank=True, null=True)
vuln_score = models.FloatField(blank=True, null=True, default=None)
reviewed = models.TextField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
videos = models.ManyToManyField(Video, through='Target', related_name="people") # Field that will have the videos of a Person
Once you have that, I suggest you to change your PersonSerializer object to be:
class PersonSerializer(CustomSerializer):
videos = VideoSerializer(source='videos', many=True)
class Meta:
model = apis_models.Person
fields = '__all__'
If you can't change your models to support ManyToMany fields I suggest you to do the following:
class Person(models.Model):
name = models.CharField(unique=True, max_length=45)
nationality = models.CharField(max_length=50, blank=True, null=True)
profession = models.CharField(max_length=50, blank=True, null=True)
avatar = models.CharField(max_length=100, blank=True, null=True)
active_scraping = models.BooleanField(blank=True, null=True)
vuln_score = models.FloatField(blank=True, null=True, default=None)
reviewed = models.TextField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
def videos(self):
return Video.objects.filter(
id__in=Target.objects.filter(person=self).values('video')
)
And use the same serializer I used in the first part of the answer.
django=2.2.3
mariadb
This error occurs after importing model from an existing database with 'inspectdb' and changed field properties.
class Post(models.Model):
post_id = models.AutoField(primary_key=True)
post_name = models.CharField(max_length=255, blank=True, null=True)
email = models.CharField(max_length=255, blank=True, null=True)
class Rank(models.Model):
rank_id = models.AutoField(primary_key=True)
rank_type = models.IntegerField(blank=True, null=True)
created_at = models.DateTimeField()
# post_id = models.IntegerField(blank=True, null=True)
post_id = models.ForeignKey(Post, on_delete=models.SET_NULL, blank=True, null=True)
Originally, it was # post_id, but I removed "managed = False", changed it to ForeignKey and then "migrate".
As far as I know that if the "post_id" in the "Post" model is "primary_key True", it replaces the "id" value with "post_id". But "Django" keeps calling up the "post_id_id".
There is no command to refer to post_id_id elsewhere.
If you have a solution or something that I'm missing, please give me some advice.
-------Add more after commented by Daniel Roseman --------
class Post(models.Model):
post_id = models.AutoField(primary_key=True)
post_name = models.CharField(max_length=255, blank=True, null=True)
email = models.CharField(max_length=255, blank=True, null=True)
class Gallery(models.Model):
uid = models.AutoField(prymary_key=True)
gallery_name = models.CharField(...)
class Rank(models.Model):
rank_id = models.AutoField(primary_key=True)
rank_type = models.IntegerField(blank=True, null=True)
created_at = models.DateTimeField()
# post_id = models.IntegerField(blank=True, null=True)
post_id = models.ForeignKey(Post, on_delete=models.SET_NULL, blank=True, null=True)
# uid = models.IntegerField(blank=True, null=True)
uid = models.ForeignKey(Gallery, on_delete=models.SET_NULL, blank=True, null=True)
Don't call your ForeignKey post_id. Call it post. The underlying database field is post_id; Django adds the _id suffix automatically.
As the title suggests, I am playing with using an existing table for the AUTH_USER_MODEL using AbstractUser. Have to --fake the migration. Any additional columns I have to add to the DB manually and then to the model as some errors come up. Not ideal.
Anyway, when I got to ./manage.py createsuperuser I get errors related to fields not existing that it requires: is_superuser, is_staff, etc. The thing is there are fields in the table for this already, just have a slightly different name. I could just change the name.
But it got me wondering if there is something built in to Django to cast an ORM field name to a table field name. Something like:
class Meta:
db_table = 'Users'
Where Django assumes the name, unless it is otherwise specified.
My quick glimpse through the documentation didn't immediately yield anything.
https://docs.djangoproject.com/en/1.11/ref/models/options/
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.
# Extend the User model
class User(AbstractUser):
id = models.AutoField(primary_key=True)
username = models.CharField(max_length=255, blank=False, null=False, unique=True)
first_name = models.CharField(max_length=255, blank=True, null=True)
last_name = models.CharField(max_length=255, blank=True, null=True)
email = models.CharField(max_length=255, blank=True, null=True)
phone = models.CharField(max_length=255, blank=True, null=True)
password = models.CharField(max_length=255, blank=True, null=True)
cono = models.IntegerField(blank=False, null=False)
ip_whitelist_1 = models.CharField(max_length=32, blank=True, null=True)
ip_whitelist_2 = models.CharField(max_length=32, blank=True, null=True)
ip_whitelist_3 = models.CharField(max_length=32, blank=True, null=True)
last_login = models.DateTimeField(blank=True, null=True)
password_changed = models.DateField(blank=True, null=True)
security_q1 = models.CharField(max_length=255, blank=True, null=True)
security_q2 = models.CharField(max_length=255, blank=True, null=True)
security_a1 = models.CharField(max_length=255, blank=True, null=True)
security_a2 = models.CharField(max_length=255, blank=True, null=True)
role = models.IntegerField(blank=True, null=True)
login_unlock_date = models.DateTimeField(blank=True, null=True)
challenge_unlock_date = models.DateTimeField(blank=True, null=True)
active = models.SmallIntegerField(blank=True, null=True)
updated = models.DateTimeField(blank=True, null=True)
created = models.DateTimeField(blank=True, null=True)
# having to add these
is_superuser = models.SmallIntegerField(blank=True, null=True)
is_staff = models.SmallIntegerField(blank=True, null=True)
is_active = models.SmallIntegerField(blank=True, null=True)
REQUIRED_FIELDS = []
USERNAME_FIELD = 'username'
is_anonymous = False
is_authenticated = True
class Meta:
db_table = 'Users'
you can try to use the db-column option:
# having to add these
is_superuser = models.SmallIntegerField(db_column="YOU_NAME1", blank=True, null=True)
# ^^^^^^
is_staff = models.SmallIntegerField(db_column="YOU_NAME2", blank=True, null=True)
# ^^^^^^
is_active = models.SmallIntegerField(db_column="YOU_NAME3", blank=True, null=True)
# ^^^^^^
I trying extend existed models so I added 'thumbnails' in to it. Unfortunately function related to this is not recognized and django console gives me:
thumbnail = models.ImageField(upload_to=_get_upload_image, blank=True, null=True)
NameError: name '_get_upload_image' is not defined
Could someone help me with this issue?
Django 1.6.5 models.py (short version)
class Feed(models.Model):
link = models.CharField(blank=True, max_length=450)
url = models.CharField(blank=True, max_length=450)
title = models.CharField(blank=True, null=True, max_length=250)
category = models.ForeignKey(Category, blank=True, null=True)
user = models.ForeignKey(User, blank=True, null=True)
last_update = models.DateField(blank=True, null=True, editable=False)
country = models.ForeignKey(Country, blank=True, null=True)
thumbnail = models.ImageField(upload_to=_get_upload_image, blank=True, null=True)
class Meta:
unique_together = (
("url", "user"),
)
def _get_upload_image(instance, filename):
return "images/%s_%S" % (str(time()).replace('.','_'), filename)
I solved this through placing function on top of class
class Feed(models.Model):
def _get_upload_image(instance, filename):
return "images/%s_%S" % (str(time()).replace('.','_'), filename)
link = models.CharField(blank=True, max_length=450)
url = models.CharField(blank=True, max_length=450)
title = models.CharField(blank=True, null=True, max_length=250)
category = models.ForeignKey(Category, blank=True, null=True)
user = models.ForeignKey(User, blank=True, null=True)
last_update = models.DateField(blank=True, null=True, editable=False)
country = models.ForeignKey(Country, blank=True, null=True)
thumbnail = models.ImageField(upload_to=_get_upload_image, blank=True, null=True)