Django使用元模型隐藏外键 [英] Django Hidden Foreign Key in form using Meta model
问题描述
我正在使用django在一个食谱网站上工作,并且在我的表单中遇到了外键字段的问题。
问题是,当我创建我的食谱我需要一个外键指向创建这个食谱的食谱,但我不想让用户创建食谱看到original_cookbook字段(他们不应该)我相信我需要使用一个小部件(HiddenInput),但是我对其他网站上给出的例子感到困惑。另外,我的朋友提到了在original_cookbook视图中设置初始值的一些方面:
tl; dr:我想将fk指向用户cookbook,同时保持original_cookbook字段隐藏。
相关代码:
形式:
class RecipeForm(forms.ModelForm):
$ b $ class Meta:
model = Recipe
$ p
模型:
pre code类$ b def __unicode __(self):
return self.name
original_cookbook = models.ForeignKey(Cookbook)
#cookbooks = models.ManyToManyField('Cookbook',related_name ='recipes')
name = models.CharField(max_length = 200)
author = models.CharField(max_length = 100)
picture = models.ImageField(upload_to ='Downloads',blank = True)
pub_date = models.DateTimeField('date date',auto_now_add = True,blank = True)
ingredients = models.T extField()
steps = models.TextField()
prep_time = models.IntegerField()
TYPE_CHOICES =(
('SW','Sandwich'),
('AP','开胃菜'),
('SD','酱汁和调料'),
('SS','汤和沙拉'),
('VG' ,'蔬菜'),
('RG','Rice,Grains and Beans'),
('PA','Pasta'),
('BR','Breakfast' ),
('MT','Meat'),
('SF','Seafood'),
('BP','Bread and Pizza'),
('DT','Desserts'),
)
type = models.CharField(max_length = 2,options = TYPE_CHOICES)
def index_queryset(self):
返回self.objects.all()
查看:
def createrecipe(request):
if request.user.is_authenticated():
return HttpResponseRedirect('/ index /')
else:
如果request.met hod =='POST':
form = RecipeForm(request.POST)
if form.is_valid():
recipe = form.save()
user = request.user
cookbooks = user.cookbooks
cookbook = cookbooks.all()[0]
cookbook.recipes.add(recipe)
return HttpResponseRedirect('/ account')
else:
form = RecipeForm()
return render_to_response('cookbook / createrecipe.html',
{'form':form},
context_instance = RequestContext (请求))
排除=('original_cookbook',)到您的表单的元
类。
然后,在你的 if form.is_valid()
代码中,执行如下操作:
....
recipe = form.save(commit = False)
recipe.original_cookbook = whatever_that_is
recipe.save()
...
I am working on a cookbook website using django and have run into a problem with a foreign key field in my form
the problem is that when i create my recipe i need to have a foreign key pointing to the cookbook that created this recipe but I don't want the user creating the recipe to see the original_cookbook field (they shouldn't have to)
I believe I need to use a widget (HiddenInput) but am I getting confused with the examples given on other sites. Also my friend mentioned something about setting an initial value in the original_cookbook view
tl;dr: I want to point the fk to the users cookbook while keeping the original_cookbook field hidden.
relevant code:
form:
class RecipeForm(forms.ModelForm):
class Meta:
model = Recipe
model:
class Recipe(models.Model):
def __unicode__(self):
return self.name
original_cookbook = models.ForeignKey(Cookbook)
#cookbooks = models.ManyToManyField('Cookbook', related_name = 'recipes')
name = models.CharField(max_length=200)
author = models.CharField(max_length= 100)
picture = models.ImageField(upload_to = 'Downloads', blank=True)
pub_date = models.DateTimeField('date published', auto_now_add=True, blank=True)
ingredients = models.TextField()
steps = models.TextField()
prep_time = models.IntegerField()
TYPE_CHOICES= (
('SW', 'Sandwich'),
('AP', 'Appetizers'),
('SD', 'Sauces and Dressings'),
('SS', 'Soups and Salads'),
('VG', 'Vegetables'),
('RG', 'Rice, Grains and Beans'),
('PA', 'Pasta'),
('BR', 'Breakfast'),
('MT', 'Meat'),
('SF', 'Seafood'),
('BP', 'Bread and Pizza'),
('DT', 'Desserts'),
)
type = models.CharField(max_length = 2, choices=TYPE_CHOICES)
def index_queryset(self):
return self.objects.all()
view:
def createrecipe(request):
if not request.user.is_authenticated():
return HttpResponseRedirect('/index/')
else:
if request.method == 'POST':
form = RecipeForm(request.POST)
if form.is_valid():
recipe = form.save()
user = request.user
cookbooks = user.cookbooks
cookbook = cookbooks.all()[0]
cookbook.recipes.add(recipe)
return HttpResponseRedirect('/account')
else:
form = RecipeForm()
return render_to_response('cookbook/createrecipe.html',
{'form':form},
context_instance=RequestContext(request))
Add exclude = ('original_cookbook',)
to your form's Meta
class.
Then, in your if form.is_valid()
code, do something like:
....
recipe = form.save(commit=False)
recipe.original_cookbook = whatever_that_is
recipe.save()
...
这篇关于Django使用元模型隐藏外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!