Slugify name field in django form -


i have model has meta: unique_together = ['slug', 'person'], person foreign key field. in form don't want type slug field. want populate child_name field. tried as:

class childform(slugcleanmixin, forms.modelform):      class meta:         model = child         fields = ('child_name','slug','child_birth_date','blood_group')          def slug(self):             return slugify(self.child_name) 

but slug field not autopopulated child_name. tried using pre_save in models as:

def create_slug(instance, new_slug=none):     slug = slugify(instance.child_name)     if new_slug not none:         slug = new_slug     qs = child.objects.filter(slug=slug).order_by("-id")     exists = qs.exists()     if exists:         new_slug = "%s-%s" %(slug, qs.first().id)         return create_slug(instance, new_slug=new_slug)     return slug   def pre_save_post_receiver(sender, instance, *args, **kwargs):     if not instance.slug:         instance.slug = create_slug(instance)  pre_save.connect(pre_save_post_receiver, sender=child) 

but nothing fulfill purpose. how that? appreciated.

my slugcleanmixin:

class slugcleanmixin:     """mixin class slug cleaning method."""      def clean_slug(self):         new_slug = (             self.cleaned_data['slug'].lower())         if new_slug == 'create':             raise validationerror(                 'slug may not "create".')         return new_slug 

my views:

class childrencreate( childrengetobjectmixin,     personcontextmixin, createview):     template_name = 'member/children_form.html'     model = child     form_class = childform       def get_initial(self):         person_slug = self.kwargs.get(             self.person_slug_url_kwarg)         self.person = get_object_or_404(             person, slug__iexact=person_slug)         initial = {             self.person_context_object_name:                 self.person,         }         initial.update(self.initial)         return initial 

models:

class child(models.model):     person = models.foreignkey(person, on_delete=models.cascade)     child_name = models.charfield(max_length=150)     slug = models.slugfield(max_length=100)     child_birth_date = models.datefield()     blood_group = models.charfield(max_length=5, blank=true)      objects = childrenmanager()       class meta:         verbose_name_plural = 'children'         ordering = ['-child_birth_date']         unique_together = ['slug', 'person'] 

i try checking out package generating slugs, i've used in several applications, , think it's great:

https://github.com/un33k/django-uuslug

they provide simple example usage, adapting models be:

class child(models.model):     child_name = models.charfield(max_length=150)     slug = models.slugfield(max_length=100)     ...      def save(self, *args, **kwargs):         self.slug = uuslug(self.child_name, instance=self)         super(child, self).save(*args, **kwargs)   

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -