How do I associate the post to a comic in custom form

43 views Asked by At

My model looks like this:

    class ComicSeries(models.Model):
        """Model definition for ComicSeries."""

        # TODO: Define fields here
        user = models.ForeignKey(User, on_delete=models.CASCADE, 
                null=True, blank=True, verbose_name='Uploaded by: '
            )
        title = models.CharField(verbose_name='Series Title', max_length=500)
        cover = models.ImageField(verbose_name='Series cover', upload_to='comic_series', 
                height_field=None, width_field=None, max_length=None
            )
        description = models.TextField(verbose_name='Description')
        artist = models.CharField(verbose_name='Artist(s)', max_length=500)
        date_uploaded = models.DateTimeField(auto_now_add=True)
        slug = models.SlugField(default='')

class ComicIssue(models.Model):
    """Model definition for ComicIssue."""

    # TODO: Define fields here
    user = models.ForeignKey(User, on_delete=models.CASCADE, 
            null=True, blank=True, verbose_name='Uploaded by: '
        )
    title = models.ForeignKey(ComicSeries, on_delete=models.CASCADE, verbose_name='Series Title')
    issue = models.CharField(verbose_name='Issue Number', max_length=500)
    issue_title = models.CharField(verbose_name='Issue Title', max_length=1000)
    issue_cover = models.ImageField(verbose_name='Issue cover', upload_to='comic_issues', height_field=None, width_field=None, max_length=None)
    issue_description = models.TextField(verbose_name='Description')
    issue_file = models.FileField(verbose_name='Issue file', upload_to='comic_issues_files', max_length=100,
        help_text='File in pdf or as single image'
    )
    is_favorite = models.BooleanField(default=False)
    issue_slug = models.SlugField(default='')

views.py :

class ComicIssueCreate(LoginRequiredMixin, CreateView):
    model = ComicIssue
    fields = ['issue_title', 'issue_cover', 'issue_description', 'issue_cover', 'issue_file']

    def form_valid(self, form):
        form.instance.user = self.request.user
        return super(ComicIssueCreate, self).form_valid(form)

I am able to select to which ComicSeries a ComicIssue belongs to in Django admin.

In django admin there is an option to upload

But on my form, there is no field when I add 'title'

Template:

{% block body %}
  <div class="container">
    <h2>Add new comic issue/chapter</h2>
    <form class="form", action="", method="POST", enctype="multipart/form-data">
      {% csrf_token %}
      {% for field in form %}
        <div class="form-group form">
            <span class="text-danger small">
                {{field.errors}}
            </span>
        </div>
        <label class="control-label col-sm-2">
            {{field.label_tag}}
            {{field.help_text}}
        </label>
        <div class="col-sm-10">{{field}}</div>
      {% endfor %}
      <button type="submit" class="btn grey-text black">Add</button>
    </form>
  </div>
{% endblock body %}

But I have a problem doing this in a custom form. Is there a way I can determine to which series an issue belongs to in a custom form using CreateView?

2

There are 2 answers

5
Ajmal Noushad On BEST ANSWER

You missed 'title' in the fields specified in the view. Since title is the foreign key of ComicIssue to ComicSeries, You need to include that in the fields to achieve whats required

    class ComicIssueCreate(LoginRequiredMixin, CreateView):
        model = ComicIssue
        fields = ['title', 'issue_title', 'issue_cover', 'issue_description', 
                  'issue_cover', 'issue_file']

        def form_valid(self, form):
           form.instance.user = self.request.user
           return super(ComicIssueCreate, self).form_valid(form)

Updates: The problem was due to not initializing the 'select' in MaterializeCSS. It is necessary to select field to work in MaterializeCSS

   <script> 
   $(document).ready(function() { 
        $('select').material_select(); 
   });
   </script>
3
cpkthompson On

The fields in ComicIssueCreate should include title

So fields = ['title','issue_title', 'issue_cover', 'issue_description', 'issue_cover', 'issue_file']