我認爲models.OneToOneField
,在這種情況下,組字段,指的是Group
表的ID字段。 但是,通過我的views.py
,該集團的id == 6
,但Group_Cover的group_id == 3
發生,如果我沒有上傳圖像時,集團的id == 2, 3 and 4
。Django的OneToOneField是否引用另一個表的ID字段?
對不起,我的英語很奇怪。 總之,我認爲Django
組6 Group_Cover的GROUP_ID時集團的ID是6
models.py
@python_2_unicode_compatible
class Group(models.Model):
#group_id = models.AutoField(primary_key=True)
group_name = models.CharField(max_length=50, unique=False, blank=False)
class Meta:
db_table = traceback.extract_stack()[-2][2].lower()
@python_2_unicode_compatible
class Group_Cover(models.Model):
group = models.OneToOneField(Group, primary_key=True)
group_cover = models.ImageField(upload_to="static/image/group/")
class Meta:
db_table = traceback.extract_stack()[-2][2].lower()
views.py
@login_required(login_url='/')
def group_register(request):
import pdb; pdb.set_trace()
from myapp.forms import GroupRegister
from myapp.forms import GroupCover
formset = GroupRegister
formcover = GroupCover
if request.method == 'POST':
form = formset(request.POST)
cover = formcover(request.POST)
if form.is_valid():
from myapp.models import Group
new_group = Group()
new_group.group_name = form.cleaned_data['group_name']
new_group.save()
from myapp.models import Group_Cover
new_cover = Group_Cover()
new_cover.group_cover = request.FILES['group_cover']
new_cover.save()
return redirect('/myapp/group_page/{}'.format(new_group.id))
else:
form = formset()
cover = formcover()
view = {
'form': form,
'cover': cover,
}
template = 'myapp/group/group_register.html'
return render(request, template, view)
謝謝哪個組,弗拉迪爾帕拉多克魯茲。我不知道。我現在嘗試。謝謝。 – yamachan
不客氣:) –