Error after posting new data to the web server


It all looks good by when i try to add some post to my topic I have an error created_by=user

enter image description here

Here is my

from django import forms
from .models import Topic

class NewTopicForm(forms.ModelForm):
    message = forms.CharField(
            attrs={'rows': 5, 'placeholder': 'What is on your mind?'}
        help_text='The max length of the text is 4000.')

    class Meta:
        model = Topic
        fields = ['subject', 'message']

and my view function for this form:

def new_topic(request, pk):
board = get_object_or_404(Board, pk=pk)
user = User.objects.first()

if request.method == 'POST':
    form = NewTopicForm(request.POST)
    if form.is_valid():
        topic =
        topic.board = board
        topic.starter = user
        post = Post.objects.create(

        return redirect('board_topics',
    form = NewTopicForm()
return render(request, 'new_topic.html', {'board': board, 'form': form})

class Post(models.Model):
    message = models.TextField(max_length=4000)
    topic = models.ForeignKey(Topic, related_name='t_posts', on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(null=True)
    created_by = models.ForeignKey(Topic, related_name='posts', on_delete=models.CASCADE)
    updated_by = models.ForeignKey(User, related_name='+', null=True, on_delete=models.CASCADE)


There is a mistake in your Post model

created_by needs to be a Foreign Key to the User table

created_by = models.ForeignKey(User, related_name='created_by', on_delete=models.CASCADE)

run the following commands after the above changes to reflect the changes in the database

python makemigrations
python migrate
Answered By: at14

What’s the best account I can have? A single person is only permitted to have one Coinbase Pro account. To avoid breaking this rule, if you already have a Coinbase account and want to create a Coinbase Pro account, use the same login email address.

Answered By: mike king
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.