Был в состоянии выполнить CRUD на филиале. Однако я застрял в создании / обновлении / удалении для компонента. Также, когда я перехожу к Testcase CRUD, какие шаги мне нужно сделать.? Пожалуйста, помогите .. Пожалуйста, дайте мне знать, если вам нужно больше информации об этом. .......
PS: я новичок в django (2 недели ... наслаждаюсь этим :-)).
Модель:
class Branch(models.Model):
"""Model representing a Branches"""
name = models.CharField(max_length=200, db_index=True, unique=True)
summary = models.CharField(max_length=200, db_index=True)
def __str__(self):
"""String for representing the Model object."""
return self.name
def get_absolute_url(self):
"""Returns the url to access a particular branch instance."""
return reverse('branch-detail', args=[str(self.id)])
class Feature(models.Model):
"""Model representing a Feature"""
branch = models.ForeignKey(Branch, on_delete=models.SET_NULL, null=True, related_name='features')
name = models.CharField(max_length=200, db_index=True, unique=True)
def __str__(self):
"""String for representing the Model object."""
return self.name
def get_absolute_url(self):
"""Returns the url to access a particular feature instance."""
return reverse('feature-detail', args=[self.branch, str(self.id)])
class Testcase(models.Model):
"""Model representing a Testcase"""
feature = models.ForeignKey(Feature, on_delete=models.SET_NULL, null=True, related_name='testcases')
name = models.CharField(max_length=200)
def __str__(self):
"""String for representing the Model object."""
return self.name
def get_absolute_url(self):
"""Returns the url to access a detail record for this testcase."""
return reverse('testcase-detail', args=[self.feature.branch, self.feature, str(self.id)])
Вид:
class BranchCreate(CreateView):
model = Branch
fields = '__all__'
success_url = reverse_lazy('index')
class BranchUpdate(UpdateView):
model = Branch
fields = ['name', 'summary']
def get_success_url(self, **kwargs):
return reverse_lazy('branch-detail', args=(self.object.pk,))
class BranchDelete(DeleteView):
model = Branch
success_url = reverse_lazy('index')
class FeatureCreate(CreateView):
model = Feature
fields = ['name', 'summary']
<<<<< BELOW CODE IS WHAT ALL METHODS I HAVE TRIED >>>>
#def form_valid(self, form):
# name = get_object_or_404(Feature, name=self.kwargs['name'])
# name1 = get_object_or_404(Feature, pk=self.kwargs['id'])
# form.instance.feature = name
# form.instance.feature = name1
# return super(FeatureCreate, self).form_valid(form)
# def get_context_object_name(self, obj):
# """Get the name to use for the object."""
# if self.context_object_name:
# return self.context_object_name
# elif isinstance(obj, Branch):
# return obj._meta.model_name
# else:
# return None
# def get_object(self, queryset=None):
# if queryset is None:
# queryset = self.get_queryset()
# return get_object_or_404(queryset, branch_id=self.kwargs['pk'])
URL:
urlpatterns = [
path('', views.index, name='index'),
path('branch/<int:pk>', views.BranchDetail.as_view(), name='branch-detail'),
path('branch/<branch>/features/', views.BranchFeatureList.as_view(), name='branch-features'),
path('branch/<branch>/feature/<int:pk>', views.FeatureDetail.as_view(), name='feature-detail'),
path('branch/<branch>/features/<feature>/testcases/', views.FeatureTestcaseList.as_view(), name='feature-testcases'),
path('branch/<branch>/features/<feature>/testcase/<int:pk>', views.TestcaseDetail.as_view(), name='testcase-detail'),
]
urlpatterns += [
path('branch/create/', views.BranchCreate.as_view(), name='branch-create'),
path('branch/<int:pk>/update/', views.BranchUpdate.as_view(), name='branch-update'),
path('branch/<int:pk>/delete/', views.BranchDelete.as_view(), name='branch-delete'),
]
urlpatterns += [
path('branch/<branch>/features/create/', views.FeatureCreate.as_view(), name='feature-create'), >>>>>>>>>> This is where I am stuck at...
# path('...../<int:pk>/update/', views.FeatureUpdate.as_view(), name='feature_update'),
# path('....../<int:pk>/delete/', views.FeatureDelete.as_view(), name='feature_delete'),
]