2017-06-05 117 views
1

我有一個Django中的模型表單很好地顯示 - 但它並沒有提供適當的信息。Django中的ModelForm - 選擇沒有填充適當的數據?

出現選擇下拉菜單,但沒有填充選項,我正在努力弄清楚原因。

我的模式是這樣的:

class Mileage(models.Model): 
    start_location = models.ForeignKey(Location, on_delete=models.PROTECT, related_name='start_locations') 
    end_location = models.ForeignKey(Location, on_delete=models.PROTECT, related_name='end_locations') 
    miles = models.DecimalField(max_digits=8, decimal_places=2) 
    user_id = models.IntegerField(null=True) 

    def __str__(self): 
     return self.miles 

class Location(models.Model): 
    name = models.CharField(max_length=255) 
    address = models.CharField(max_length=255, null=True) 
    latitude = models.DecimalField(max_digits=9, decimal_places=6, null=True) 
    longitude = models.DecimalField(max_digits=9, decimal_places=6, null=True) 
    user_id = models.IntegerField(null=True) 

    def __str__(self): 
     return self.id 

class Trip(models.Model): 
    start_location = models.CharField(max_length=255) 
    end_location = models.CharField(max_length=255) 
    miles = models.DecimalField(max_digits=7, decimal_places=2) 
    user = models.ForeignKey(User, on_delete=models.PROTECT) 
    trip_date = models.DateTimeField('trip date') 

    def __str__(self): 
     return self.id 

我的ModelForm看起來是這樣的:

class TripsForm(ModelForm): 
    class Meta: 
     model = Trip 
     fields = ['start_location', 'end_location', 'miles', 'trip_date'] 
     widgets = { 
      'start_location': forms.Select(
       attrs={ 
        'class': 'form-control', 
        'id': 'start_location' 
       } 
      ), 
      'end_location': forms.Select(
       attrs={ 
        'class': 'form-control', 
        'id': 'end_location' 
       } 
      ), 
      'miles': forms.NumberInput(
       attrs={ 
        'class': 'form-control', 
        'id': 'miles', 
        'readonly': 'readonly' 
       } 
      ), 
      'trip_date': forms.TextInput(
       attrs={ 
        'class': 'form-control monthpicker datepicker', 
        'id': 'trip_date' 
       } 
      ), 
     } 

在我看來,我打電話像這樣從view.py

# Create trip 
def trip_create(request): 
    # if this is a POST request we need to process the form data 
    if request.method == 'POST': 
     form = TripsForm(request.POST) 
     # check whether it's valid: 
     if form.is_valid(): 
      # Save the form -- will handle this all later 
      trip.save() 
      return HttpResponseRedirect('/thanks/') 
    # if a GET (or any other method) we'll create a blank form 
    else: 
     form = TripsForm() 
     # Return to trips with list of date 
    return render(request, 'trips/create.html', {'form': form}) 

'start_locations'和'end_locations'選項應該填入所有位置 - curren這些選擇完全是空的。

我一直在尋找通過文檔:是否modelformset_factory()是在這種情況下挖掘更多的東西?

我只是不知道如何着手讓它填充這些下拉菜單。

+0

可以肯定的是,數據庫中有一些位置,對不對? – FamousJameous

+0

您可以提供您的view.py – marin

+0

是@FamousJameous - 數據庫中大約有27個位置。我將很快更新view.py – Hanny

回答

1

當前您使用的CharFieldTrip.start_locationTrip.end_location。如果您使用選擇小部件,您可以創建選項來填充它。

如果您的start_locationend_location字段使用ForeignKey,那麼Django會爲您填充選項。

class Trip(models.Model): 
    start_location = models.ForeignKey(Location, on_delete=models.PROTECT, related_name='trip_start_locations') 
    end_location = models.ForeignKey(Location, on_delete=models.PROTECT, related_name='trip_end_locations') 

在模型中進行更改後,您必須進行遷移,然後進行遷移。

請注意,我已經包括在related_nametrip,因爲相關的名稱是用來把你從Location模型回Trip。您可能需要將related_name更新爲您的Mileage型號,以包含mileage

+0

出於好奇 - 有沒有一種方法,我可以指定下拉菜單顯示位置名稱,而不是位置的ID? – Hanny

+0

請參閱[此問題](https://stackoverflow.com/questions/6754776/django-change-select-items-display/6754902#6754902)。 – Alasdair