I'm writing a simple Django powered notetaking app, and currently I'm implementing basic tagging system for my notes. The requirement is to have a Tag model with added_by field on it as a ForeignKey to User. First I have tried to modify django-taggit source, as well as extend its Tag model, however haven't had any success. So, I decided to implement my own tagging system from scratch. For this purpose I'm using a ManyToManyField:
class Note(models.Model):
slug = models.SlugField()
title = models.CharField(max_length=255)
[...]
tags = models.ManyToManyField(Tag)
This is my form:
class NoteForm(forms.ModelForm):
class Meta:
model = Note
fields = [
'title',
'description',
'notebook',
'tags'
]
widgets = {
'tags': forms.TextInput()
}
Everything renders nicely, but when I try to submit the create form, I get an error:
So, inside that TextInput is a comma separated string. I assume that's not what Django expects, but I don't know how to convert that into something Django would accept.
I have tried to debug this, but it seems that my form_valid isn't getting executed at all. I have also tried to put some debug statements in clean_tags in my ModelForm, which is also not getting executed.
How can I overcome the issue?
My view (that currently doesn't handle any logic related to tags):
class NoteCreateView(LoginRequiredMixin, CreateView):
model = Note
form_class = NoteForm
def form_valid(self, form):
print(self.request.POST)
form.instance.added_by = self.request.user
return super().form_valid(form)
def get_initial(self):
return {'notebook': self.kwargs.get('pk')}
def get_success_url(self):
return reverse('notes:note-list', kwargs={'pk': self.kwargs['pk']})
Related
I have a pretty basic definition within my forms.py to pull data from my submitted form. Something I have done with other apps, but for some reason, it is not working at the moment. I feel like I'm overlooking something. Whenever I print(image_height) I get None.
models.py
height_field = models.IntegerField(default=1200)
forms.py
class PostForm(ModelForm):
class Meta:
model = Post
fields = ['user', 'title', 'content', 'post_image',
'height_field', 'width_field', 'draft', 'publish']
def clean_post_image(self):
post_image = self.cleaned_data["post_image"]
image_height = self.cleaned_data.get("height_field")
print(image_height)
return post_image
If you want to validate your form field with respect to other field, you should do that in your clean method.
From the docs,
The form subclass’s clean() method can perform validation that requires access to multiple form fields. This is where you might put in checks such as “if field A is supplied, field B must contain a valid email address”. This method can return a completely different dictionary if it wishes, which will be used as the cleaned_data.
You could do something like this,
def clean(self, cleaned_data):
post_image = cleaned_data.get("post_image")
height_image = cleaned_data.get("height_image")
#do_your_thing
return cleaned_data
My Content model has a many-to-many relationship to the Tag model. When I save a Content object, I want to add the relationships dynamically. I'm doing this the following way.
def tag_content(obj):
for tag in Tag.objects.all():
print tag
obj.tags.add(tag)
obj.is_tagged = True
obj.save()
class Tag(models.Model):
name = models.CharField(max_length=255)
class Content(models.Model):
title = models.CharField(max_length=255)
is_tagged = models.BooleanField(default=False)
tags = models.ManyToManyField(Tag, blank=True)
def save(self, *args, **kwargs):
super(Content, self).save(*args, **kwargs)
#receiver(post_save, sender = Content)
def update_m2m_relationships_on_save(sender, **kwargs):
if not kwargs['instance'].is_tagged:
tag_content(kwargs['instance'])
The tag_content function runs, however, the m2m relationships are not established. Im using Django 1.9.8 btw. This makes no sense. What am I missing? Moreover, if I do something like tag_content(content_instance) in shell, then the tags are set, so the function is ok. I guess the problem is in the receiver. Any help?
Edit
My question has nothing to do with m2m_changed, as I have said, creating a Content object in shell works perfectly. Therefore, the problem lies in the admin panel's setup.
Ok so I solved the problem. Basically, this has something to do with how Django handles its form in the admin panel. When trying to add the Contents from admin, I kept the tags field empty, thinking the tag_content function would handle it. However, that is exactly where the problem was, as creating a Content from shell tagged it just fine. In other words, changing the admin panel to something like this solved my problem :
from django.contrib import admin
from myapp.models import *
from django import forms
class ContentCreationForm(forms.ModelForm):
class Meta:
model = Content
fields = ('title',)
class ContentChangeForm(forms.ModelForm):
class Meta:
model = Content
fields = ('title', 'is_tagged', 'tags')
class ContentAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
if obj is None:
return ContentCreationForm
else:
return ContentChangeForm
admin.site.register(Tag)
admin.site.register(Content, ContentAdmin)
When trying to create a new Content, only the 'title' field is presented. This solves the problem.
Been trying to determine the "most" elegant solution to dropping a field from a from if the user is not is_staff/is_superuser. Found one that works, with a minimal amount of code. Originally I though to add 'close' to the 'exclude' meta or use two different forms. But this seems to document what's going on. The logic is in the 'views.py' which is where I feel it blongs.
My question: Is this safe? I've not seen forms manipulated in this fashion, it works.
models.py
class Update(models.Model):
denial = models.ForeignKey(Denial)
user = models.ForeignKey(User)
action = models.CharField(max_length=1, choices=ACTION_CHOICES)
notes = models.TextField(blank=True, null=True)
timestamp = models.DateTimeField(default=datetime.datetime.utcnow().replace(tzinfo=utc))
close = models.BooleanField(default=False)
forms.py
class UpdateForm(ModelForm):
class Meta:
model = Update
exclude = ['user', 'timestamp', 'denial', ]
views.py
class UpdateView(CreateView):
model = Update
form_class = UpdateForm
success_url = '/denials/'
template_name = 'denials/update_detail.html'
def get_form(self, form_class):
form = super(UpdateView, self).get_form(form_class)
if not self.request.user.is_staff:
form.fields.pop('close') # ordinary users cannot close tickets.
return form
Yes, your approach is perfectly valid. The FormMixin was designed so you can override methods related to managing the form in the view and it is straightforward to test.
However, should yours or someone else's dynamic modifications of the resulting form object become too extensive, it would probably be best to define several form classes and use get_form_class() to pick the correct form class to instantiate the form object from.
I took a look at following SO question, but had no luck. I don't know, maybe I didn't understand the answers.
1) How to remove the “Currently” tag and link of a FileInput widget in Django?
2) Django ModelForm ImageField
My form:
class SettingsForm(forms.ModelForm):
company_logo = forms.ImageField(label=_('Company Logo'),required=False, error_messages = {'invalid':_("Image files only")})
class Meta:
model = Settings
fields = ("company_logo")
....
My model:
class Settings(models.Model):
strg=CustomFileSystemStorage(strict_name='images/company_logo.png',save_format='PNG')
company_logo=models.ImageField(upload_to='images',blank=True,null=True,storage=strg)
.....
After rendering:
I see from the first link, that the models.ImageField inherits the FileInput and adds the extra stuff, but I do not understand how to overcome this?
Thanks in advance.
The solution is:
class SettingsForm(forms.ModelForm):
company_logo = forms.ImageField(label=_('Company Logo'),required=False, error_messages = {'invalid':_("Image files only")}, widget=forms.FileInput)
class Meta:
model = Settings
fields = ("company_logo")
....
I added the widget forms.FileInput, in order to tell the ImageField to use the basic field, not the one inherited from FileInput.
#mtndesign, you might also want a "remove" option, which you can place wherever you like in your template.
class MyForm(forms.ModelForm):
photo = forms.ImageField(required=False, widget=forms.FileInput)
remove_photo = forms.BooleanField(required=False)
...
def save(self, commit=True):
instance = super(MyForm, self).save(commit=False)
if self.cleaned_data.get('remove_photo'):
try:
os.unlink(instance.photo.path)
except OSError:
pass
instance.photo = None
if commit:
instance.save()
return instance
You can change the widget used to render the form field by specifying it on initializing:
class SettingsForm(forms.ModelForm):
company_logo = forms.ImageField(label=_('Company Logo'),required=False, \
error_messages ={'invalid':_("Image files only")},\
widget=FileInput)
See the docs for widgets.
A foreign key on a model is not appearing in the Django admin site. This is irrespective of whether the field is explicitly specified in a ModelAdmin instance (fields = ('title', 'field-that-does-not-show-up')) or not.
I realize there are many variables that could be causing this behavior.
class AdvertiserAdmin(admin.ModelAdmin):
search_fields = ['company_name', 'website']
list_display = ['company_name', 'website', 'user']
class AdBaseAdmin(admin.ModelAdmin):
list_display = ['title', 'url', 'advertiser', 'since', 'updated', 'enabled']
list_filter = ['updated', 'enabled', 'since', 'updated', 'zone']
search_fields = ['title', 'url']
The problem is the advertiser foreign key is not showing up in the admin for AdBase
class Advertiser(models.Model):
""" A Model for our Advertiser
"""
company_name = models.CharField(max_length=255)
website = models.URLField(verify_exists=True)
user = models.ForeignKey(User)
def __unicode__(self):
return "%s" % self.company_name
def get_website_url(self):
return "%s" % self.website
class AdBase(models.Model):
"""
This is our base model, from which all ads will inherit.
The manager methods for this model will determine which ads to
display return etc.
"""
title = models.CharField(max_length=255)
url = models.URLField(verify_exists=True)
enabled = models.BooleanField(default=False)
since = models.DateTimeField(default=datetime.now)
expires_on=models.DateTimeField(_('Expires on'), blank=True, null=True)
updated = models.DateTimeField(editable=False)
# Relations
advertiser = models.ForeignKey(Advertiser)
category = models.ForeignKey(AdCategory)
zone = models.ForeignKey(AdZone)
# Our Custom Manager
objects = AdManager()
def __unicode__(self):
return "%s" % self.title
#models.permalink
def get_absolute_url(self):
return ('adzone_ad_view', [self.id])
def save(self, *args, **kwargs):
self.updated = datetime.now()
super(AdBase, self).save(*args, **kwargs)
def impressions(self, start=None, end=None):
if start is not None:
start_q=models.Q(impression_date__gte=start)
else:
start_q=models.Q()
if end is not None:
end_q=models.Q(impression_date__lte=end)
else:
end_q=models.Q()
return self.adimpression_set.filter(start_q & end_q).count()
def clicks(self, start=None, end=None):
if start is not None:
start_q=models.Q(click_date__gte=start)
else:
start_q=models.Q()
if end is not None:
end_q=models.Q(click_date__lte=end)
else:
end_q=models.Q()
return self.adclick_set.filter(start_q & end_q).count()
class BannerAd(AdBase):
""" A standard banner Ad """
content = models.ImageField(upload_to="adzone/bannerads/")
The mystery deepens. I just tried to create a ModelForm object for both AdBase and BannerAd, and both generated fields for the advertiser. Some crazy admin things going on here...
I believe I've just run into exactly the same problem, but was able to debug it thanks to the help of persistent co-workers. :)
In short, if you look in the raw HTML source you'll find the field was always there - it's just that:
Django tries to be clever and put the form field inside a div with CSS class="form-row $FIELD_NAME",
The field's name was "advertiser", so the CSS class was "form-row advertiser",
...Adblock Plus.
Adblock Plus will hide anything with the CSS class "advertiser", along with a hell of a lot of other CSS classes.
I consider this a bug in Django.
maybe it is an encode error. I had the same problem, but when i added # -- coding: UTF-8 -- in the models.py, all fine.
Another very dumb cause of the same problem:
If there is only one instance of the related model, then the filter simply won't show. There is a has_output() method in RelatedFieldListFilter class that returns False in this case.
It's a strange problem for sure. On the AdBase model if you change
advertiser = models.ForeignKey(Advertiser)
to
adver = models.ForeignKey(Advertiser)
then I believe it'll show up.
Powellc, do you have the models registered with their respective ModelAdmin class?
admin.site.register(Advertiser, AdvertiserAdmin) after the ModelAdmin definitions.
You are talking about the list_display option, right?
Is the unicode-method for your related model set?
If the field is a ForeignKey, Django
will display the unicode() of the
related object
Also check this thread for some hints: Can "list_display" in a Django ModelAdmin display attributes of ForeignKey fields?
Try disabling your ad blocker. No, this is not a joke. I just ran into this exact problem.
We just ran into this problem.
It seems that if you call you field advertiser the in the admin the gets given an 'advertiser' class.
Then is then hidden by standard ad blocking plugins. If you view source your field will be there.