Models unique_together constraint + None = fail? - django

2 questions:
How can I stop duplicates from being created when parent=None and name is the same?
Can i call a model method from within the form?
Please see full details below:
models.py
class MyTest(models.Model):
parent = models.ForeignKey('self', null=True, blank=True, related_name='children')
name = models.CharField(max_length=50)
slug = models.SlugField(max_length=255, blank=True, unique=True)
owner = models.ForeignKey(User, null=True)
class Meta:
unique_together = ("parent", "name")
def save(self, *args, **kwargs):
self.slug = self.make_slug()
super(MyTest, self).save(*args, **kwargs)
def make_slug(self):
# some stuff here
return generated_slug
note: slug = unique as well!
forms.py
class MyTestForm(forms.ModelForm):
class Meta:
model = MyTest
exclude = ('slug',)
def clean_name(self):
name = self.cleaned_data.get("name")
parent = self.cleaned_data.get("parent")
if parent is None:
# this doesn't work when MODIFYING existing elements!
if len(MyTest.objects.filter(name = name, parent = None)) > 0:
raise forms.ValidationError("name not unique")
return name
Details
The unique_together contraint works perfectly w/ the form when parent != None. However when parent == None (null) it allows duplicates to be created.
In order to try and avoid this, i tried using the form and defined clean_name to attempt to check for duplicates. This works when creating new objects, but doesn't work when modifying existing objects.
Someone had mentioned i should use commit=False on the ModelForm's .save, but I couldn't figure out how to do/implement this. I also thought about using the ModelForm's has_changed to detect changes to a model and allow them, but has_changed returns true on newly created objects with the form as well. help!
Also, (somewhat a completely different question) can I access the make_slug() model method from the Form? I believe that currently my exclude = ('slug',) line is also ignoring the 'unique' constraint on the slug field, and in the models save field, I'm generating the slug instead. I was wondering if i could do this in the forms.py instead?

You could have a different form whether you are creating or updating.
Use the instance kwarg when instantiating the form.
if slug:
instance = MyTest.object.get( slug=slug )
form = MyUpdateTestForm( instance=instance )
else:
form = MyTestForm()
For the second part, I think that's where you could bring in commit=False, something like:
if form.is_valid():
inst = form.save( commit=False )
inst.slug = inst.make_slug()
inst.save()

I don't know for sure this will fix your problem, but I suggest testing your code on the latest Django trunk code. Get it with:
svn co http://code.djangoproject.com/svn/django/trunk/
There have been several fixes to unique_together since the release of 1.02, for example see ticket 9493.

Unique together should be a tuple of tuples
unique_together = (("parent", "name"),)

Related

override delete_selected to check for records that exist in other tables in django 1.10

I am using the default admin dashboard of django. Basically, i want to override the delete_selected method on a model by model basis so I can check for records before allowing the deletion to take place.
My models.py is:
class Kind(models.Model):
name = models.CharField(max_length=50, unique=True)
addedby = models.ForeignKey(User,related_name='admin_kind')
createdon = models.DateTimeField(auto_now_add=True)
updatedon = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
class Meta:
ordering = ('name',)
class Item(models.Model):
name = models.CharField(max_length=50)
kind = models.ForeignKey(Kind,related_name="item_kind")
createdon = models.DateTimeField(auto_now_add=True)
updatedon = models.DateTimeField(auto_now=True)
def __str__(self):
return ' , '.join([self.kind.name, self.name])
class Meta:
ordering = ('kind', 'name',)
unique_together = (('name','kind'))
Now what I want is before a kind could be deleted, I want to check if there is a related record in items. If there is, do not delete it.
But i am stuck at how to go about overriding delete_selected method in admin.py.
def delete_selected(self, request, obj):
'''
Delete Kind only if there are no items under it.
'''
for o in obj.all():
featuredItems = Item.objects.filter(kind=o).count()
if featuredItems == 0:
o.delete()
However, django shows the warning and when i click yes, it deletes the kind even though there are records for it. I want to absolutely block the deletion.
Actually you are trying to write a lot of code for something that can be done merely by adding an attribute to your model field
PROTECT
Prevent deletion of the referenced object by raising ProtectedError, a subclass of django.db.IntegrityError.
class Item(models.Model):
name=models.CharField(max_length=50)
kind=models.ForeignKey(Kind,related_name="item_kind", on_delete=models.PROTECT)
What you are trying to achieve is made even more difficult by the fact that django displays a confirmation page on delete.
Function code is correct, but you need to explicitly tell django to use your own function to delete that model's objects. You can do that by declaring a list in your admin.py,
actions = ['delete_selected']
Where "delete_selected" is your function name.

M2M using through and form with multiple checkboxes

I'd like to create a form allowing me to assign services to supplier from these models. There is no M2M relationship defined since I use a DB used by others program, so it seems not possible to change it. I might be wrong with that too.
class Service(models.Model):
name = models.CharField(max_length=30L, blank=True)
class ServiceUser(models.Model):
service = models.ForeignKey(Service, null=False, blank=False)
contact = models.ForeignKey(Contact, null=False, blank=False)
class SupplierPrice(models.Model):
service_user = models.ForeignKey('ServiceUser')
price_type = models.IntegerField(choices=PRICE_TYPES)
price = models.DecimalField(max_digits=10, decimal_places=4)
I've created this form:
class SupplierServiceForm(ModelForm):
class Meta:
services = ModelMultipleChoiceField(queryset=Service.objects.all())
model = ServiceUser
widgets = {
'service': CheckboxSelectMultiple(),
'contact': HiddenInput(),
}
Here is the view I started to work on without any success:
class SupplierServiceUpdateView(FormActionMixin, TemplateView):
def get_context_data(self, **kwargs):
supplier = Contact.objects.get(pk=self.kwargs.get('pk'))
service_user = ServiceUser.objects.filter(contact=supplier)
form = SupplierServiceForm(instance=service_user)
return {'form': form}
I have the feeling that something is wrong in the way I'm trying to do it. I have a correct form displayed but it is not instantiated with the contact and checkboxes aren't checked even if a supplier has already some entries in service_user.
You are defining services inside your Meta class. Put it outside, right after the beginning of SupplierServiceForm. At the very least it should show up then.
Edit:
I misunderstood your objective. It seems you want to show a multiple select for a field that can only have 1 value. Your service field will not be able to store the multiple services.
So, by definition, your ServiceUser can have only one Service.
If you don't want to modify the database because of other apps using it, you can create another field with a many to many relationship to Service. That could cause conflicts with other parts of your apps using the old field, but without modifying the relationship i don't see another way.
The solution to my problem was indeed to redefine my models in oder to integrate the m2m relationship that was missing, using the through argument. Then I had to adapt a form with a special init method to have all selected services displayed in checkboxes, and a special save() method to save the form using m2m relationship.
class Supplier(Contact):
services = models.ManyToManyField('Service', through='SupplierPrice')
class Service(models.Model):
name = models.CharField(max_length=30L, blank=True)
class ServiceUser(models.Model):
service = models.ForeignKey(Service, null=False, blank=False)
supplier = models.ForeignKey(Supplier, null=False, blank=False)
price = models.Decimal(max_digits=10, decimal_places=2, default=0)
And the form, adapted from the very famous post about toppings and pizza stuff.
class SupplierServiceForm(ModelForm):
class Meta:
model = Supplier
fields = ('services',)
widgets = {
'services': CheckboxSelectMultiple(),
'contact_ptr_id': HiddenInput(),
}
services = ModelMultipleChoiceField(queryset=Service.objects.all(), required=False)
def __init__(self, *args, **kwargs):
# Here kwargs should contain an instance of Supplier
if 'instance' in kwargs:
# We get the 'initial' keyword argument or initialize it
# as a dict if it didn't exist.
initial = kwargs.setdefault('initial', {})
# The widget for a ModelMultipleChoiceField expects
# a list of primary key for the selected data (checked boxes).
initial['services'] = [s.pk for s in kwargs['instance'].services.all()]
ModelForm.__init__(self, *args, **kwargs)
def save(self, commit=True):
supplier = ModelForm.save(self, False)
# Prepare a 'save_m2m' method for the form,
def save_m2m():
new_services = self.cleaned_data['services']
old_services = supplier.services.all()
for service in old_services:
if service not in new_services:
service.delete()
for service in new_services:
if service not in old_services:
SupplierPrice.objects.create(supplier=supplier, service=service)
self.save_m2m = save_m2m
# Do we need to save all changes now?
if commit:
self.save_m2m()
return supplier
This changed my first models and will make a mess in my old DB but at least it works.

Django: why the manytomany choice box only has on side

I have extended the group model, where I added some manytomany fields, and in the admin page, it likes this:
However, what I expected is this:
Here is how I implemented the m2m field:
class MyGroup(ProfileGroup):
mobile = models.CharField(max_length = 15)
email = models.CharField(max_length = 15)
c_annotates = models.ManyToManyField(Annotation, verbose_name=_('annotation'), blank=True, null=True)
c_locations = models.ManyToManyField(Location, verbose_name=_('locations'), blank=True, null=True)
And in the database there is a relational form which contains the pairs of group_id and location_id.
Is there anyone who knows how to do it? Thanks!
EDIT:
I implemented as above, the multiple select box actually shows up, but it cannot save... (Sorry, I was working on a virtual machine and it's offline now, so I have to clip the code from screen)
latest 2017
govt_job_post is model having qualification as ManyToMany field.
class gjobs(admin.ModelAdmin):
filter_horizontal = ('qualification',)
admin.site.register(govt_job_post, gjobs)
Problem solved. It can save the multiple choice field now.
class GroupAdminForm(forms.ModelForm):
users = forms.ModelMultipleChoiceField(queryset=User.objects.all(),
widget=FilteredSelectMultiple('Users', False),
required=False)
locations = forms.ModelMultipleChoiceField(queryset=Location.objects.all(),
widget=FilteredSelectMultiple('Location', False),
required=False)
class Meta:
model = Group
def __init__(self, *args, **kwargs):
instance = kwargs.get('instance', None)
if instance is not None:
initial = kwargs.get('initial', {})
initial['users'] = instance.user_set.all()
initial['locations'] = instance.c_locations.all()
kwargs['initial'] = initial
super(GroupAdminForm, self).__init__(*args, **kwargs)
def save(self, commit=True):
group = super(GroupAdminForm, self).save(commit=commit)
if commit:
group.user_set = self.cleaned_data['users']
group.locations = self.cleaned_data['locations']
else:
old_save_m2m = self.save_m2m
def new_save_m2m():
old_save_m2m()
group.user_set = self.cleaned_data['users']
group.location_set = self.cleaned_data['locations']
self.save_m2m = new_save_m2m
return group
Either I am overlooking something that makes your situation unusual or you are making it harder than it needs to be. Since you're using the admin, the vast majority of the code necessary to use the admin's more convenient multiselects is already available. All you should need to do is declare your ManyToMany fields, as you have, and then include those fields in your admin class's filter_horizontal attribute. Or filter_vertical if you want the boxes stacked, but your screenshot shows the horizontal case.
This by itself does not require a custom form for your admin.

Django: Automatically setting empty strings in model fields to None?

I want to iterate over my model fields within a Django model and check if they are empty string and replace them with a null in the model save() method programmatically. This is because some CharFields need to be unique or have no value.
Example:
class Person(models.Model):
name = models.CharField(blank=True, unique=True, null=True)
nick_name = models.CharField(blank=True, unique=True, null=True)
...
age = models.IntegerField()
def save(self, *args, **kwargs):
for field in self._meta.fields: # get the model fields
if field=='':
field = None
super(Person, self).save(*args, **kwargs)
The above complains about a creation_counter which is unclear why an attempt to compare those values instead of the field value with the empty string is done.
This could be done manually, but I have too many models....
Any suggestions?
edit:
Thanks to everyone who attempted to help me! :D
The solution that seems to work for me is posted by Jazz, but his code isn't showing up in his post. This is my version which is essentially identical, but with an extra check to make sure we are only overriding when necessary:
from django.db.models.Field import CharField as _CharField
class CharField(_CharField):
def get_db_prep_value(self, value, *args, **kwargs):
if self.blank == self.null == self.unique == True and value == '':
value = None
return super(CharField).get_db_prep_value(value, *args, **kwargs)
In your case, I would suggest a custom model field, which subclasses a CharField and ensures that a empty string is converted to None -- overriding get_db_prep_value should do it.
class EmptyStringToNoneField(models.CharField):
def get_prep_value(self, value):
if value == '':
return None
return value
class Person(models.Model):
name = EmptyStringToNoneField(blank=True, unique=True, null=True)
nick_name = EmptyStringToNoneField(blank=True, unique=True, null=True)
https://docs.djangoproject.com/en/dev/howto/custom-model-fields/
Jazz's answer works like a charm for me.
I just want to point out that if you are using South migration tool, the schemamigration command will fail, unless you specify a introspect rule for the new custom field.
For "simple fields", you can simply add the following code to wherever your field is specified.
from south.modelsinspector import add_introspection_rules
add_introspection_rules([], ["^myapp\.stuff\.fields\.SomeNewField"])
Now the South migration should work.
FYI.
Reference: http://south.aeracode.org/wiki/MyFieldsDontWork

How to hide model field in Django Admin?

I generate field automaticly, so I want to hide it from user. I've tried editable = False and hide it from exclude = ('field',). All this things hide this field from me, but made it empty so I've got error: null value in column "date" violates not-null constraint.
models.py:
class Message(models.Model):
title = models.CharField(max_length = 100)
text = models.TextField()
date = models.DateTimeField(editable=False)
user = models.ForeignKey(User, null = True, blank = True)
main_category = models.ForeignKey(MainCategory)
sub_category = models.ForeignKey(SubCategory)
groups = models.ManyToManyField(Group)`
admin.py:
class MessageAdminForm(forms.ModelForm):
def __init__(self, *arg, **kwargs):
super(MessageAdminForm, self).__init__(*arg, **kwargs)
self.initial['date'] = datetime.now()
class MessageAdmin(admin.ModelAdmin):
form = MessageAdminForm
list_display = ('title','user',)
list_filter = ('date',)
Based on your model setup, I think the easiest thing to do would change your date field to:
date = models.DateTimeField(auto_now=True)
that should accomplish what you're after and you don't even need to exclude it from the admin, it's excluded by default. If you have auto_now=True it will act as a 'last update time'. If you have auto_now_add=True it will act as a creation time stamp.
There are several other ways you could accomplish your goal if your use case is more complex than a simple auto date field.
Override the model's save method to put the value in.
class Message(models.Model):
title=models.CharField(max_length=100)
date = models.DateTimeField(editable=False)
def save(*args, **kwargs):
self.date = datetime.datetime.now()
super(Message, self).save(*args, **kwargs)
What you are trying to do with the Model Admin isn't quite working because by default django only transfers the form fields back to a model instance if the fields are included. I think this might be so the model form doesn't try to assign arbitrary attributes to the model. The correct way to accomplish this would be to set the value on the instance in your form's save method.
class MessageAdminForm(forms.ModelForm):
def save(*args, **kwargs):
self.instance.date = datetime.now()
return super(MessageAdminForm, self).save(*args, **kwargs)