我建立了模式。 py Internal Profile_management app.
import requests
from django.db import models
from datetime import date
from package_management.models import Package
from api_url_config import CREATE_CONNECTION_API_URL
class ClientProfile(models.Model):
id = models.AutoField(primary_key=True, editable=False)
profile_name = models.CharField(max_length=50, default="default_profile")
active_status = models.BooleanField(choices=[(True, Yes ), (False, No )], default=True)
subscribed_package = models.ForeignKey(Package, related_name= client_profiles_subscribe , on_delete=models.PROTECT,
default=1)
plugin_platform = models.CharField(max_length=15, blank=True, null=True)
expire_date = models.DateField(default=date.today)
company_email = models.EmailField(unique=True, null=True)
company_phone = models.CharField(max_length=15, blank=True, null=True)
website = models.CharField(max_length=50, null=True)
company_address = models.TextField(max_length=255, blank=True, null=True)
main_client = models.BooleanField(choices=[(True, Yes ), (False, No )], default=True)
def profile_auto_increment_id(self):
if self.pk:
return f"#{self.pk}"
else:
return "(Not saved)"
profile_auto_increment_id.short_description = ID
def __str__(self):
return self.profile_name
def save(self, *args, **kwargs):
self.send_data_to_api()
def send_data_to_api(self):
api_url = CREATE_CONNECTION_API_URL
data = {
"profile_name": self.profile_name,
"active_status": self.active_status,
"subscribed_package": self.subscribed_package.id,
"plugin_platform": self.plugin_platform,
"expire_date": str(self.expire_date),
"company_email": self.company_email,
"company_phone": self.company_phone,
"website": self.website,
"company_address": self.company_address,
"main_client": self.main_client,
}
try:
response = requests.post(api_url, json=data)
response.raise_for_status()
except requests.exceptions.RequestException as e:
print(f"Error sending data to API: {e}")
在此,我没有在数据库中保存数据,就将数据发送至APIC。 此前,我把数据储存在我的当地数据库中,因此,我可以把这些数据显示在行政上:
from django.contrib import admin
from django.urls import reverse
from django.utils.html import format_html
from profile_management.models import ClientProfile
@admin.register(ClientProfile)
class ProfileAdmin(admin.ModelAdmin):
list_display = (
profile_auto_increment_id , profile_name , profile_status , action_column )
search_fields = ( profile_name ,)
list_filter = ( profile_status , )
def profile_auto_increment_id(self, obj):
if obj.pk:
return f"#{obj.pk}"
else:
return "(Not saved)"
profile_auto_increment_id.short_description = ID
def profile_auto_increment_id_link(self, obj):
if obj.pk:
return f"#{obj.pk}"
else:
return "(Not saved)"
profile_auto_increment_id_link.admin_order_field = id
profile_auto_increment_id_link.short_description = ID
def action_column(self, obj):
# This method is for the action column
edit_url = reverse( admin:profile_management_clientprofile_change , args=[obj.pk])
return format_html(
<a href="{}" class="button">Edit</a> , edit_url
)
action_column.short_description = Actions
现在我如何能够从APIC获得数据并显示行政数据表? 例见APIC/L.2/Add.1。