views:

23

answers:

0

Hi,

How can I create a Form with normal form elements and generic elements together as ModelForm For using frontend CRUD.

For example;

Generic Model:

class Todo(models.Model):  
  user = models.ForeignKey(User, related_name="todo")   
  title = models.CharField(max_length=100, unique=False)
  slug = models.SlugField(max_length=50)
  completed = models.BooleanField()
  created_at = models.DateTimeField(auto_now_add=True)
  updated_at = models.DateTimeField(auto_now_add=True)  

  content_type = models.ForeignKey(ContentType, related_name="todos")
  object_id = models.PositiveIntegerField()
  content_object = generic.GenericForeignKey()

And other model:

class Project(models.Model):     
  user = models.ForeignKey(User, related_name="project")
  title = models.CharField(max_length=255, unique=True)
  slug = models.SlugField(max_length=50)
  description = models.TextField()  
  active = models.BooleanField()
  created_at = models.DateTimeField(auto_now_add=True)
  updated_at = models.DateTimeField(auto_now_add=True)         

  # Generic relation with Todo model
  todo = generic.GenericRelation(Todo)

Forms:

Created a formset for making Todo inline for Project and passed formset to form template. But its not merged with ProjectForm when render form.

  from django.contrib.contenttypes.generic import generic_inlineformset_factory
  TodoFormSet = generic_inlineformset_factory(Todo, extra=1)
  formset = TodoFormSet(instance=Project())

Now if I create a ModelForm,

for project: (project/create urlpattern, with this i can get ProjectForm to produce form)

for todo: (project/todo/create urlpattern, with this i can get TodoForm to produce todo)

How to i do ProjectForm inline TodoForm form? and produce new ProjectForm?

Thanks.