Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

7
How to write a create/update view for an unmanaged model?
Post Body

I have an unmanaged model named 'Dummy'. I want to render a form named 'DummyForm' to either create or update records in the table. But I keep getting an error "cursor "_django_curs_10804_sync_1" does not exist". How can I solve this error? Any guidance would be greatly appreciated.

I am not sure if there is something wrong in my view as I am trying to create and update using the same view.

models.py:

class Dummy(models.Model):
    firstname = models.CharField(max_length=255)
    lastname = models.CharField(max_length=255)
    age = models.IntegerField()

    class Meta:
        managed = False
        db_table = 'dummy'

forms.py

class DummyForm(forms.ModelForm):
    class Meta:
        model = Dummy
        fields = '__all__'

views.py

def dummy_update(request, pk):
    instance = Dummy.objects.using('postgresdb').get(pk=pk)

    if request.method == 'POST':
        if instance:
            form = DummyForm(request.POST, instance=instance)
        else:
            form = DummyForm(request.POST)

        if form.is_valid()
            form.save(using='postgresdb')
            return redirect('homepage')
    else:
        if instance:
            form = DummyForm(instance=instance)
        else:
            form = DummyForm()

    return render(request, 'dummy_update.html', {'form': form}

dummy_update.html template

{% block content %}
    <div class="container">
        <form method="POST" enctype="multipart/form-data">
            {% csrf_token %}

            {{ form | crispy }}

            <button class="btn btn-outline-success" type="submit">Update</button>
        </form>
    </div>
{% endblock content %}

Author
User Disabled
Account Strength
0%
Disabled 4 months ago
Account Age
5 years
Verified Email
Yes
Verified Flair
No
Total Karma
6,531
Link Karma
663
Comment Karma
5,737
Profile updated: 4 days ago
Posts updated: 1 year ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
4 years ago