2 years ago

#74387

test-img

Charles Ballantyne

Forigen Key not displaying in rendered model form

I am looking for guidance on where I am going wrong. My model form is rendering correctly however the author has no option to select or input. I suspect this is is because the author is a foreign key. How do I get around this so the form will display a list of users or allow to manually input a name. Any help would be greatly appreciated :)

models.py class Post(models.Model):

    title = models.CharField(max_length=200, unique=True)
    slug = models.SlugField(max_length=200, unique=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE,
                               related_name="workout_posts")
    updated_on = models.DateTimeField(auto_now=True)
    featured_image = CloudinaryField('image', default='placeholder')
    content = models.TextField(default='SOME STRING')
    excerpt = models.TextField(blank=True)
    created_on = models.DateTimeField(auto_now_add=True)

    class Meta:
        # ordered created on field starting with newest first
        ordering = ['-created_on']

    def __str__(self):
        return self.title

forms.py

class MakeWorkOutForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = '__all__'

views.py

def createWorkOut(request):
    form = MakeWorkOutForm
    context = {'form': form}
    return render(request, "add-workout.html", context)

html

{% extends "base.html" %} {% block content %} {% load static %} 
<h1>Create A Workout</h1>
<form action="" method="POST">
    {% csrf_token %}
    {{form}}
    <input type="submit" name="submit">
</form>
{% endblock content %}

python

django

forms

model

0 Answers

Your Answer

Accepted video resources