简体   繁体   中英

Updating a Django Model from DetailView

I have a Django model that is receiving most of it's variable values from a form created by User 1 using CreateView. User 1 creates an object and selects most field values.

Once the object is created it is posted to a wall, another user (User 2) can access the object and add information for the final unfilled field. My question is, how do I allow for this functionality in the DetailView html form? In the code below, "basketball.salary2" is the previously empty field User 2 will be inputting and posting in the DetailView. All other values are already filled in and displaying in the HTML.

basketball_detail.html:

{% extends "base.html" %}

{% block title %}By: {{ basketball.creator }}{% endblock %}

{% block content %}

<h2>{{ basketball.creator }} has placed an opinion:</h2>
<p>{{ basketball.creator }} says that {{ basketball.player }} deserves 
  {{ basketball.salary1 }} 

<p> Do you agree with {{ basketball.creator }}? </p>

<p>I believe {{ basketball.player }} deserves {{ basketball.salary2 }} <p>



{% endblock content %}

views.py:

from django.shortcuts import render
from django.views.generic import ListView, CreateView, DetailView
from django.contrib.auth.mixins import LoginRequiredMixin

from .models import Basketball

class BasketballListView(LoginRequiredMixin, ListView):
    model = Basketball


class BasketballDetailView(DetailView):
    model = Basketball

class BasketballCreateView(LoginRequiredMixin, CreateView):
    model = Basketball
    fields = [
    'player',
    'salary1',
    'salary2',
    ]

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

models.py:

from django.db import models
from django.urls import reverse
from django.conf import settings
from django.utils.text import slugify

from autoslug import AutoSlugField
from model_utils.models import TimeStampedModel

from nba_api.stats.static import players

PLAYER_CHOICES = [(i['full_name'], i['full_name']) for i in players.get_active_players()]


class Basketball(TimeStampedModel):
    player = models.CharField(max_length = 50, choices = PLAYER_CHOICES, default = None)
    slug = AutoSlugField("Basketball Player", unique = True, populate_from ="player")
    salary1 = models.DecimalField(decimal_places = 2, max_digits = 15)
    salary2 = models.DecimalField(decimal_places = 2, max_digits = 15)
    creator = models.ForeignKey(settings.AUTH_USER_MODEL,
    null = True,
    on_delete = models.SET_NULL
    )

    def get_absolute_url(self):
        return reverse(
        'basketball:detail', kwargs={'slug': self.slug}
        )

I think for this case UpdateView will work instead of DetailView .

class BasketballUpdateView(UpdateView):
    model = Basketball

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM